最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - How do I call javascript just before a response redirect - Stack Overflow

programmeradmin5浏览0评论

I'm trying to run some java script just before a page redirect but it fails to run. When I comment out the Response.Redirect all works fine but this goes against the particular requirements. Any ideas on how to implement this functionality?

        Dim strscript As String = "<script>alert('hello');</script>"

        If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
            ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
        End If

        Response.Redirect("")  

I'm trying to run some java script just before a page redirect but it fails to run. When I comment out the Response.Redirect all works fine but this goes against the particular requirements. Any ideas on how to implement this functionality?

        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")  
Share Improve this question asked Mar 12, 2009 at 17:39 TonyNeallonTonyNeallon 6,60712 gold badges44 silver badges49 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 12

Your problem is that the Response.Redirect redirects the response (...) before anything is sent back to the client. So what the client gets is a response from Google rather than from your server.

In order to write some javascript on the page and have it execute before sending the client to Google, you'll need to do your redirect in javascript after the alert.

   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

The client isn't getting a chance to load. Try redirecting from the client side:

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

If you want to execute some javascript before redirecting, you will need to do the redirect in javascript and not in 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

Not Working:

string sScript = "<script language='javascript'>alert(\"" + Alertstr + "\");  alert('Record has been Updated Successfully'); </script>";
ClientScript.RegisterStartupScript(typeof(Page), "alert", sScript);
response.redirect("LandingPage.aspx");

Working:

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);

If you use a Response.Redirect it actually sends a 3XX response to the browser which causes it to send a request to the URL in the redirect. It won't actually load/render any data contained with the response (actually I don't think any data is sent). If you want it to redirect after the page loads, you may want to either include a META refresh header that redirects a certain amount of time after load or use javascript do to the redirect at the end of your script.

发布评论

评论列表(0)

  1. 暂无评论