Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Browser HTML5/CSS
Hi i am looking for a code that can check the browser and version of a visitor.
Based on the outcome it should direct it to the HTML5 supported website otherwise the code needs to direct the user to annon compliant website.
 
This is my code, but it's not working:
 
<html>
  <head>
  <title>Browser detection</Title>
  <Script Language="JavaScript">
  // Detect the browsername
    browsername=navigator.appName;
    if (browsername.indexOf("Netscape")!=-1) {browsername="NS"}
    else
    {
      if (browsername.indexOf("Microsoft")!=-1) {browsername="MSIE"}
      else {browsername="N/A"}
    };
 
    //detect the browserversion
    browserversion="0";
    if (navigator.appVersion.indexOf("2.")!=-1) {browserversion="2"};
    if (navigator.appVersion.indexOf("3.")!=-1) {browserversion="3"};
    if (navigator.appVersion.indexOf("4.")!=-1) {browserversion="4"};
    if (navigator.appVersion.indexOf("5.")!=-1) {browserversion="5"};
    if (navigator.appVersion.indexOf("6.")!=-1) {browserversion="6"};
 
    // Send visitor to relevant pages
    if (browsername=="NS") {window.location="http://www.yahoo.com"};
    if (browsername=="MSIE"){
      if (browserversion<4){window.location="http://www.hotbot.com"}
      else {window.location="http://www.msn.com"}
    }
    if (browsername=="N/A") {window.location="http://www.webcrawler.com"};
  </script>
  </head>
  <body>
  </body>
</html>
 
Thanks a lot!
filip
Posted 13-Mar-13 4:49am
Edited 13-Mar-13 5:37am
v2
Comments
joshrduncan2012 at 13-Mar-13 10:20am
   
This isn't a question. Are you wanting someone to write the code for you? I doubt anyone will do that for you here.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

it works now.
 
<html>
<head>
<title>Browser detection</Title>
<Script Language="JavaScript">
// Detect the browsername
browsername=navigator.appName;
if (browsername.indexOf("Netscape")!=-1) {browsername="NS"}
else
{if (browsername.indexOf("Microsoft")!=-1) {browsername="MSIE"}
else {browsername="N/A"}};
 
//detect the browserversion
browserversion="0";
if (navigator.appVersion.indexOf("2.")!=-1) {browserversion="2"};
if (navigator.appVersion.indexOf("3.")!=-1) {browserversion="3"};
if (navigator.appVersion.indexOf("4.")!=-1) {browserversion="4"};
if (navigator.appVersion.indexOf("5.")!=-1) {browserversion="5"};
if (navigator.appVersion.indexOf("6.")!=-1) {browserversion="6"};
 
// Send visitor to relevant pages
if (browsername=="NS") {window.location="http://www.yahoo.com"};
if (browsername=="MSIE"){
  if (browserversion<4){window.location="http://www.hotbot.com"}
  else {window.location="http://www.msn.com"}
}
if (browsername=="N/A") {window.location="http://www.webcrawler.com"};
</script>
</head>
 
<body>
</body>
</html>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use modernizr[^] instead.
Quote:
Modernizr is a JavaScript library that detects HTML5 and CSS3 features in the user’s browser.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 319
1 Maciej Los 295
2 Sergey Alexandrovich Kryukov 230
3 CPallini 205
4 Sinisa Hajnal 166
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 13 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100