Solved

Check browser type and redirect accordingly using javascript

Posted on 2007-12-03
10
4,359 Views
Last Modified: 2012-05-05
Hi, thanks for looking at my question.

I am looking for some javascript that will detect the users browser and then redirect them.
This is for an internal web application in my company and we only use IE6 and greater, therefore I would like the check to do this:

IF BROWSER NOT =IE6 OR IE7 then

REDIRECT to NOT_IE.ASP

end if

I have looked on the net but I cant seem to find code that I can modify myself (not being great with J/script does not help much! )

Can someone help please?

many thanks

PDM
0
Comment
Question by:Pigdogmonster
  • 4
  • 3
  • 3
10 Comments
 
LVL 6

Expert Comment

by:CasUK
ID: 20395002
To detect the browser and redirect use the following code - it's up to you what browsers you want to detect. Also, You can detect the O/S platform, which I've included.

And so on...
// Browser Name
var broswer = navgator.appName;
 
//Browser description
var description = navigator.userAgent;
 
//Display the details
alert(browser + "\n\n" + description);
 
// Now redirect
if (browser == "Microsoft Internet Explorer"){
  window.location = "myiepage.html";
} elseif (browser == "Netscape"){
  window.location = "mynetscapepage.html";
}

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20395999
You DO have to spell browser correctly though...

<html>
<head>
<script>
if (navigator.userAgent.toLowerCase().indexOf('msie') ==-1) location.replace('not_ie.asp');
</script>
</head>

Open in new window

0
 
LVL 6

Expert Comment

by:CasUK
ID: 20396027
lol. Yes:)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Pigdogmonster
ID: 20402431
Hi Gents,

Thanks for the comments.  what do I actually need in my page then..

here's what I have at the moment...

With which I get an error, saying missing ";"


<script>
 
// Browser Name
var broswer = navgator.appName;
 
//Browser description
var description = navigator.userAgent;
 
//Display the details
alert(browser + "\n\n" + description);
 
// Now redirect
if (browser == "Microsoft Internet Explorer"){
  window.location = "myiepage.html";
} elseif (browser == "Netscape"){
  window.location = "mynetscapepage.html";
}
 
 
if (navigator.userAgent.toLowerCase().indexOf('msie') ==-1) location.replace('not_ie.asp');
 
</script>

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 50 total points
ID: 20402652
elseif is not valid javascript
broswer is spelled browser

My code had been enough - especially when spelling navigator correctly too:

<script>
if (navigator.userAgent.toLowerCase().indexOf('msie') ==-1) location.replace('not_ie.asp');
</script>

Open in new window

0
 
LVL 6

Expert Comment

by:CasUK
ID: 20410181
Yep,

Don't mix the two scripts because they both do the same thing - I have just added comments and an alert in my one so you can see what's going on.

As mplungjan pointed out - there's were a few typos in my original script - it shows that you shouldn't rush these things!

Both do the same job - so choose one of the other and you'll be fine.
Oh, and I've corrected my original script...
<script type="text/javascript">
 
//Browser Name
var browser = navigator.appName;
 
//Browser description - what browser and platfor you're using if interested
var description = navigator.userAgent;
 
//Display the details (if you want to see what you're using before a redirect)
alert(browser + "\n\n" + description);
 
// Now redirect
if (browser == "Microsoft Internet Explorer"){
  window.location = "myiepage.html";
} else if (browser == "Netscape"){
  window.location = "mynetscapepage.html";
}
</script>

Open in new window

0
 

Author Comment

by:Pigdogmonster
ID: 20418286
Thanks very much for your scripts and time gents, I have used mplungjan's script as its very simple... just one thing though.. I have tested this script on Firefox and it does the redirect as it should but I have just tested it on an Opera browser and it didnt redirect?  any ideas?

Thanks again.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20422831
<script>
var notIe = navigator.userAgent.toLowerCase().indexOf('msie') ==-1;
var isOpera = navigator.userAgent.toLowerCase().indexOf('opera') !=-1;
if (notIE || isOpera) location.replace('not_ie.asp');
</script>
0
 

Author Comment

by:Pigdogmonster
ID: 20426664
hey,

I tried that and Opera still has access?

The Opera browser details are:
Version 9.24
Build 8816
Plattform Win32

any ideas?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20436472
I need to know what it says when you paste this into the location bar and press enter:

javascript:alert(navigator.userAgent)

thanks

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question