Check browser type and redirect accordingly using javascript

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
PigdogmonsterAsked:
Who is Participating?
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
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
 
CasUKCommented:
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
 
Michel PlungjanIT ExpertCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
CasUKCommented:
lol. Yes:)
0
 
PigdogmonsterAuthor Commented:
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
 
CasUKCommented:
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
 
PigdogmonsterAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
<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
 
PigdogmonsterAuthor Commented:
hey,

I tried that and Opera still has access?

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

any ideas?
0
 
Michel PlungjanIT ExpertCommented:
I need to know what it says when you paste this into the location bar and press enter:

javascript:alert(navigator.userAgent)

thanks

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.