Home » none » ASP.NET Response.Redirect() and JavaScript

ASP.NET Response.Redirect() and JavaScript

A toucan perched on a branch in Brazil. Yesterday we covered issues surrounding using ASP.NET’s Response.Redirect in server side code. We noted that not handing it correctly could prevent code from running on the server that we want to run.

The other issue is emitting Javascript in the server side in association with Response.Redirect(). This also leads to unexpected problems if you aren’t thinking about what is actually happening with your code.

Take this code as an example:

    protected void Page_Load(object sender, EventArgs e)
        string myscript = @"<script language='javascript'>
alert('hello world');
            ("".GetType(), "s", myscript);


The question is, why does the javascript never display “hello world”?

Actually, the javascript is typically a little more complicated than “hello world.” But the question is always, “Why didn’t my javascript execute? It works fine without the redirect.”

Once again, we need to think more clearly about what we’ve actually written.

What we’ve actually told the server to do is the following:

  1. Render the javascript to display “hello world” in an alert box on the client.
  2. Set the header (not the header element, but the header that tells the browser whether the code executed successfully or not) to “302 redirect.”

When the browser finally gets the stream back from the server, it will actually see step 2 first because the header comes before the javascript code. The browser will look at that 302, ignore everything else on the page, and faithfully redirect to the page specified as part of the 302.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Response.Redirect() executes too soon on the Server.Response.Redirect() executes too soon on the Server. I've seen this question a couple of times in various situations. The first involves Javascript and the second involves server side code. Both are caused by a misunderstanding of what […]
  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it […]
  • Infinite 302 Loop – How would you even know?Infinite 302 Loop – How would you even know? It happens eventually to all web developers.  It's happened to me twice in the last week.  You make one simple change to your web site and then you can no longer access it.  When you […]
  • DotNetNuke Response.End() Doesn’t EndDotNetNuke Response.End() Doesn’t End OK.  This one is just plain annoying!For some reason that I’ve yet to be able to track down, Response.End() seems to have no impact at all under DotNetNuke.I tried various hacks.  […]
  • ASP.NET Cross Domain Form SubmissionASP.NET Cross Domain Form Submission Not to be confused with cross page posting, cross domain submission allows us to post the contents of an ASP.NET form to a completely different domain. To achieve this we will need to […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

  • Noor

    how to prevent this and follow the initial plan described in above steps.

  • Dave

    Use a javascript redirect after the javascript you want to see run:

    window.location = “urlToRedirectTo”;