views:

2585

answers:

5

Hi there, 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")
+1  A: 

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");&lt;/script&gt;"

If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
   ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
End If
Chris Pebble
+1  A: 

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';&lt;/script&gt;"

If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
    ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
End If
Darin Dimitrov
+5  A: 

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'&lt;/script&gt;"

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

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.

tvanfosson
Actually the redirect sends a "page moved" page along with the 3xx code. That's what the browser shows if you get into an eternal redirect loop and the browser gives up after 20 or so redirects.
Guffa
Ok. So none of the data you've added to the page....
tvanfosson
@tvanfosson: You *can* send your own response body with a 3xx status code, but 99.9% of the users won't see it (because their browser will redirect without rendering it). I usually send a link with the same destination as the redirection, for those unfortunate souls which don't get redirected automatically.
Piskvor
A: 

Not Work: string sScript = "alert(\"" + Alertstr + "\"); alert('Record has been Updated Successfully'); "; ClientScript.RegisterStartupScript(typeof(Page), "alert", sScript); response.redirect("LandingPage.aspx");

Working : string sScript = "alert(\"" + Alertstr + "\"); alert('Record has been Updated Successfully'); window.location.href = 'LandingPage.aspx'; "; ClientScript.RegisterStartupScript(typeof(Page), "alert", sScript);

siva