Solved

Check browser type and redirect accordingly using javascript

Posted on 2007-12-03
10
4,357 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now