Asp.net 如何在响应重定向之前调用javascript
我正试图在页面重定向之前运行一些java脚本,但它无法运行。 当我注释掉响应时,重定向可以正常工作,但这违背了特定的要求。关于如何实现这个功能有什么想法吗Asp.net 如何在响应重定向之前调用javascript,asp.net,javascript,vb.net,asp.net-2.0,Asp.net,Javascript,Vb.net,Asp.net 2.0,我正试图在页面重定向之前运行一些java脚本,但它无法运行。 当我注释掉响应时,重定向可以正常工作,但这违背了特定的要求。关于如何实现这个功能有什么想法吗 Dim strscript As String = "<script>alert('hello');</script>" If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
Dim strscript As String = "<script>alert('hello');</script>"
If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
End If
Response.Redirect("http://www.google.com")
Dim strscript As String=“警报('hello');”
如果不是ClientScript.IsClientScriptBlockRegistered(“ClientScript”),则
RegisterStartupScript(Me.GetType(),“ClientScript”,strscript)
如果结束
响应。重定向(“http://www.google.com")
客户端没有机会加载。尝试从客户端重定向:
Dim strscript As String = "<script>alert('hello');window.location.href("http://www.google.com");</script>"
If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
End If
Dim strscript As String=“alert('hello');window.location.href(“http://www.google.com");"
如果不是ClientScript.IsClientScriptBlockRegistered(“ClientScript”),则
RegisterStartupScript(Me.GetType(),“ClientScript”,strscript)
如果结束
如果要在重定向之前执行一些javascript,则需要在javascript中而不是在ASP.NET中执行重定向
Dim strscript As String = "<script>alert('hello'); window.location.href='http://www.google.com';</script>"
If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
End If
Dim strscript As String=“alert('hello');window.location.href=”http://www.google.com';"
如果不是ClientScript.IsClientScriptBlockRegistered(“ClientScript”),则
RegisterStartupScript(Me.GetType(),“ClientScript”,strscript)
如果结束
您的问题在于响应。重定向会在将任何内容发送回客户端之前重定向响应(…)。因此,客户端得到的是来自Google的响应,而不是来自您的服务器的响应
为了在页面上编写一些javascript并在将客户端发送到Google之前执行它,您需要在发出警报后在javascript中执行重定向
Dim strscript As String = "<script>alert('hello');window.location.href='http://www.google.com'</script>"
If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
End If
Dim strscript As String=“alert('hello');window.location.href=”http://www.google.com'"
如果不是ClientScript.IsClientScriptBlockRegistered(“ClientScript”),则
RegisterStartupScript(Me.GetType(),“ClientScript”,strscript)
如果结束
如果使用响应。重定向,它实际上会向浏览器发送一个请求,从而使浏览器向重定向中的URL发送一个请求。它实际上不会加载/呈现响应中包含的任何数据(实际上我认为不会发送任何数据)。如果您希望在页面加载后重定向,您可以在加载后包含重定向一定时间的脚本,或者在脚本末尾使用javascript do进行重定向。不起作用:
string sScript = "<script language='javascript'>alert(\"" + Alertstr + "\"); alert('Record has been Updated Successfully'); </script>";
ClientScript.RegisterStartupScript(typeof(Page), "alert", sScript);
response.redirect("LandingPage.aspx");
string sScript=“警报(\”“+Alertstr+”\”);警报(‘记录已成功更新’);”;
RegisterStartupScript(typeof(Page),“alert”,sScript);
重定向(“LandingPage.aspx”);
工作:
string sScript = "<script language='javascript'>alert(\"" + Alertstr + "\"); alert('Record has been Updated Successfully'); window.location.href = 'LandingPage.aspx'; </script>";
ClientScript.RegisterStartupScript(typeof(Page), "alert", sScript);
string sScript=“alert(\”“+Alertstr+”\”);alert(‘记录已成功更新’);window.location.href=‘LandingPage.aspx’;”;
RegisterStartupScript(typeof(Page),“alert”,sScript);
实际上,重定向会发送一个“页面移动”页面以及3xx代码。如果您进入一个永久重定向循环,并且浏览器在大约20次重定向后放弃,则浏览器会显示这一点。您可以使用3xx状态代码发送自己的响应正文,但99.9%的用户不会看到它(因为他们的浏览器将在不渲染的情况下重定向)。我通常发送一个与重定向目的地相同的链接,用于那些没有自动重定向的不幸灵魂。