News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

Redirection based on browser detected by JavaScript

Started by ben2ong2, October 04, 2006, 03:57:31 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2


Okay, now that you have learnt browser detection, we shall see how to implement it to redirect visitors to different pages depending on their browser.

<SCRIPT LANGUAGE="JavaScript" TYPE="TEXT/JAVASCRIPT">
<!--

var bname = navigator.appName;
if (bname.search(/netscape/i) == 0)
   {
   window.location="nn.html";
   }
else if (bname.search(/microsoft/i) == 0)
   {
   window.location="ie.html";
   }
else
   {
   window.location="other_browsers.html";
   }

//-->
</SCRIPT>
After assigning the browser name to bname, we search for the presence of specific words using JavaScript Regular Expressions. (JavaScript Regular expressions have been included in version 1.2 of the language and are based on those found in the Perl language. So if you were familiar with Perl, picking up the JavaScript regular expressions would be very simple).
The search() method searches for the string specified between the two forward slashes "/". The 'i' modifier makes it possible for case sensitive searches.
The JavaScript If tests whether condition is true or false. Thus, Netscape users are redirected to nn.html, Internet Explorer users to ie.html and other browsers to "other_browsers.html".
The above code has to be placed inside the HTML head. It can also be contained inside a function which can be called using the onLoad().

You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login