Browser detection is commonly done in JavaScript because browsers, in particular
Internet Explorer and Netscape, often differ in terms of what JavaScript statements
work in the particular browser. By detecting what browser the user is
employing, one can then either have IE code segments and Netscape code segments
on the same page, or different pages -- one for IE, one for Netscape.
To illustrate the latter approach, the following link will take you to a page
which detects which browser is being used and then uses the location.href property
to redirect the user to an IE or Netscape page.
Code for redirect page using location.href
Redirection page using window.location.href
The disadvantage of using the window.location.href property for redirection
in this situation is that the back button on the browser is effectively disabled,
because clicking the back button from the page after the redirection returns
to the page from where the redirection occurred, which sends one back to the
current page. The window.location.replace method is therefore better in
these situations, because the new page replaces the old page (where the redirection
occurred) in the history list. Then clicking the back button skips over
the page where the redirection occurred, because it is no longer in the history
list.
Code for redirect page using location.replace()
window.location.replace()
In the former case, the code is window.location.href = "newpage.html" whereas in the latter case it is window.location.replace("newpage.html") illustrating the differences between coding of properties and methods.
In the former case, the browser detection is done as follows:
if (navigator.appName.indexOf("Microsoft") != -1)
window.location.href = "iepage.html";
if (navigator.appName.indexOf("Netscape") != -1) window.location.href
= "netscape.html";
The code for the latter is the same, with the change from location.href to location.replace()
Revised: June 12, 2002 Comments to William Pegram, wpegram@nvcc.edu