Redirecting Visitors to Other Pages

Now that you understand browser detection, you can tailor your site to provide information specific to each browser. There are two main ways to do this. First, you can use document.write(), which we saw in the last chapter, to display one message on your page if the site visitor is using Netscape Navigator 4, and a different message on the same page for Internet Explorer 6.0. Alternatively, you can redirect your visitors to separate pages specifically tailored to different browsers. To redirect visitors to another page, you'd write something like this:

window.location.href = "http://www.mywebsite.com/page_for_netscape4.html";

When JavaScript sees a line like this, it loads the page with the specified URL into ...

Get The Book of JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.