As far as I know, we can use two method to redirects http response.
- Using Java script window.location;
- Using HttpContext.Current.Response.Redirect in ASP.Net
From function and end user perspective, are the two methods interchangeable?
As far as I know, we can use two method to redirects http response.
From function and end user perspective, are the two methods interchangeable?
window.location
requires both javascript and for the browser to download and render the original page's contents (including css/scripts) first before the page is changed.Response.Redirect
, on the other hand, issues a 302 status code with a Location header. This causes the browser to instantly request the next page without downloading/rendering the original first.Based on your other question, it's worth noting that
window.location
has the benefit of being able to execute other javascript before the location is changed. For example, changing the location of another frame AND the current frame at the same time.I'm not familiar with
HttpContext.Current.Response.Redirect
, but I guess it issues an HTTP 301 response or something similar.HTTP response codes are ALWAYS preferred, because they are built-in to, well, HTTP. Everybody understands them and they always work. Search engines and other automated apps respect them as well.
The Javascript method on the other hand does not always work and is non-standard.
Furthermore, with HTTP codes the transferred data is kept to a minimum, while the Javascript method always needs to load a whole page.
EDIT: To illustrate:
This is all that needs to be transferred for an HTTP redirect to work, the standard HTTP header:
For Javascript, it's more like this:
A complete HTML document needs to be transferred and evaluated, which will take a lot longer and is not understood by anything but Javascript-savvy browsers.