後で別のページにリダイレクトしたときに、コード内のJavaScriptを機能させるにはどうすればよいですか?私はaspボタンコントロールを持っています。私がそのボタンをクリックすると、警告を出し、別のページに移動します。自分のコード(JSコードの前後)にResponse.Redirect
があると、8回の試行のどれも動作しません。私がリダイレクトするとコメントすると、少数(2,7 & 8)が動作します。リダイレクトを伴うJavaScriptアラートのコード
//Try one
ScriptManager.RegisterStartupScript(this, GetType(), "test", "alert('test1');", true);
//Try two
ClientScript.RegisterClientScriptBlock(typeof(Page), "test", "test2");
//Try three
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "alertMessage()", true);
//Try four
ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "alertMessage()", true);
//Try five
ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "javascript: alertMessage(); ", true);
//Try six
ClientScript.RegisterClientScriptBlock(GetType(), "CallMyFunction", "<script>alert('test4')</script>");
//Try seven
Response.Write("<script>alert('test5');</script>");
//Try eight
string script = "alert('test6')";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "CallMyString", script, true);
response.redirect("pageUrlHere");
//With this code above, none of the js functions (alerts) work
//response.redirect("pageUrlHere");
//With this commented out, try 2, 7 and 8 work.
JS機能:
function alertMessage() {
alert('test3');
}
ありがとうございます! –
心配しないで、うれしいことに助けてくれました:) – chridam
小さな変更は、ページ名 'ScriptManager.RegisterStartupScript(this、this.GetType()、" redirect "、 " alert( 'test 9');の前にスラッシュが必要です。 "Request.ApplicationPath +" /anotherpage.aspx ";"); – n00b