views:

147

answers:

2

Hello everyone,

As far as I know, we can use two method to redirects http response.

  1. Using Java script window.location;
  2. Using HttpContext.Current.Response.Redirect in ASP.Net

From function and end user perspective, are the two methods interchangeable?

thanks in advance, George

+4  A: 

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.

Richard Szalay
Thanks Richard, I am confused about what do you mean "window.location requires both javascript and for the browser to download" -- for the browser to download you mean download what? Sorry English is not my native language and appreciate if you could say the same thing in some other words. :-)
George2
The browser needs to download the HTML for the page that included the "window.location" javascript. In doing so, it needs to download the HTML and any related CSS/Javascript before it can start processing the HTML / Javascript, at which time it will execute the window.location call and change URL. Response.Redirect will skip all of that and the browser will just load the new page.
Richard Szalay
Will the javascript solution also create a record in the browse history, while the response.redirect won't?
Alohci
Thanks Richard, from end user perspective, are the two solutions of the same effect?
George2
If the javascript is at the top of the html (in the head tag) then there is no difference (except that it will take slightly longer than using Response.Redirect). If the javascript is at the bottom of the html (near /body) then the user will see the original page appear before the second page is loaded.
Richard Szalay
Thanks Richard! How about comparing to use http meta refresh? From end user perspective, the same effect?
George2
window.location and meta refresh have similar experiences
Richard Szalay
Thanks @Richard, I remembered there is a term called client initialized redirection, so window.location and http meta are both client initialized redirection and using response Location header is server initialized redirection?
George2
That's correct.
Richard Szalay
Thanks Richard, question answered.
George2
+4  A: 

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:

HTTP/1.1 301 Moved Permanently
Location: http://somenewlocation.com/

For Javascript, it's more like this:

HTTP/1.1 200 OK
Date: Wed, 22 Jul 2009 07:56:14 GMT
Server: Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Transfer-Encoding: chunked
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <script>window.location = "somewhere.com"</script>
</head>
<body>
</body>
</html>

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.

deceze
Thanks deceze, why you say "The Javascript method on the other hand does not always work and is non-standard."? Actually I see a lot of people and code using that method. :-)
George2
It is not part of the HTTP spec and requires the browser has javascript enabled.
Richard Szalay
For the Javascript way to work you need something that understands Javascript. A browser usually does, but you can deactivate/block JS in the browser too. Search engines will listen to HTTP response codes, but won't know what to do with JS. It's not just Javascript-savvy browser that read webpages. :)
deceze
Thanks Richard and deceze, from end user perspective (not targeted for search engine or bots), the two methods have the same function and always interchangeable?
George2
If it is a non-indexed (by search engines) and internal site where the browser is controlled then yes, they are interchangable.
Richard Szalay
(though I'd still recommend Response.Redirect if you don't need to change multiple frames)
Richard Szalay
Response.Redirect issues a 302 "Moved Temporarily" response, not a 301 - which may or may not be what you need - 9 times out of 10 it probably is, but sometimes you need that extra permanence of a 301, and then you'll need to craft the response headers yourself. (see code in http://stackoverflow.com/questions/150329/404-page-that-displays-requested-page/150460#150460 for an example)
Zhaph - Ben Duguid
Also, if it *was* a 301, then no, the two methods are **NOT** inter-changable - a 301 should really be interpreted by a browser as "Direct all future requests to this new page, I never want to see you here again": http://tools.ietf.org/html/rfc2616#section-10.3.2
Zhaph - Ben Duguid
I was just using `301` as an example, substitute it with any code you want/need... :)
deceze
Hi Richard, what do you mean "internal site where the browser is controlled"? Especially why you specially mentioned internal site and what do you mean controlled?
George2
Ben and deceze, but from end user perspective, are they of the same effect?
George2
Internal site == intranet. Not available to the browser public.
Richard Szalay
@George2: The Javascript can potentially cause a noticeable delay for the user. Also, as you asked elsewhere, the site will probably go into the browser history. Also, if there's any content on the page, it may show up for an instant.
deceze
@Richard, I did not quite catch your points why you specifically mentioned internal site? For redirection should no differences between internet and intranet?
George2
@deceze and @Ben, appreciate if you could let me know what is the differences between 301 and 302? I read the ietf documents but still very confused.
George2
@Ben, I read the Stack Overflow question you mentioned in your reply, but I did not find any relationship between this question you mentioned and the question I am asking. Appreciate if you could let me know the relationship? :-)
George2
My internal site reference was related to whether you can expect your users to have javascript enabled or not.
Richard Szalay
`301` basically says "this address might've been okay, but please don't use it anymore, use this one instead", `302` just says "please look over here for now" without discouraging using the original address in the future. The difference is mostly moot unless you're indexing sites (e.g. search engines). http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
deceze
Thanks @deceze, could I understand 302 as a temporary suggestion for redirection and 301 as a permanent suggestion for redirection, but from end user perspective, are there any different experience?
George2
@Richard, in my limited experience, javascript is always enabled :-)
George2
@George2 Exactly. The user doesn't notice whether it's a `301` or `302`, it just redirects. You should use a `301` for example to redirect `www.mysite.com` to `mysite.com`, to make everyone understand that you prefer them to use the non-`www` version. For most other temporary redirects use `302`.
deceze
Thanks deceze! You are a guru!
George2