我如何让后台JavaScript在重定向到另一页后继续工作?我有一个asp按钮控件,当我点击该按钮时,我希望弹出警告,然后导航到另一页。 当我的代码中有
Response.Redirect
(在JS代码之前或之后),8次尝试中没有一次有效。 当我注释掉重定向代码时,其中几个(2,7和8)有效。//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');
}
ScriptManager.RegisterStartupScript(this,this.GetType(),"redirect", "alert('test 9'); window.location='" + Request.ApplicationPath + "/anotherpage.aspx';",true);
- n00b