Redirecting Users

They have: 40 posts

Joined: Jul 2001

Hi all. I am having a little bit of trouble coding for my site to be viewed in both Netscape and IE. I have several Javascript features in my site that only work in newer versions of the browsers. I remember reading along time ago about redirecting users depending on the browser that they are using. I know you must build two sets of pages which is no problem but I don't know the line of code to make this happen... Any suggestions?

Thanks!

Brian Farkas's picture

They have: 1,015 posts

Joined: Apr 1999

This will check the browser, and if it's after version 4, the user will be redirected to domain.com/good.html. If before version 4, user will be redirected to domain.com/bad.html. Place this code in the head section:

<script language="JavaScript">
<!--
function MM_checkBrowser(NSvers,NSpass,NSnoPass,IEvers,IEpass,IEnoPass,OBpass,URL,altURL) { //v3.0
  var newURL='', verStr=navigator.appVersion, app=navigator.appName, version = parseFloat(verStr);
  if (app.indexOf('Netscape') != -1) {
    if (version >= NSvers) {if (NSpass>0) newURL=(NSpass==1)?URL:altURL;}
    else {if (NSnoPass>0) newURL=(NSnoPass==1)?URL:altURL;}
  } else if (app.indexOf('Microsoft') != -1) {
    if (version >= IEvers || verStr.indexOf(IEvers) != -1)
     {if (IEpass>0) newURL=(IEpass==1)?URL:altURL;}
    else {if (IEnoPass>0) newURL=(IEnoPass==1)?URL:altURL;}
  } else if (OBpass>0) newURL=(OBpass==1)?URL:altURL;
  if (newURL) { window.location=unescape(newURL); document.MM_returnValue=false; }
}
//-->
&lt;/script&gt;
'

And add this to the "body" tag: onLoad="MM_checkBrowser(4.0,1,2,4.0,1,2,2,'http://www.yoursite.com/good.html','http://www.yoursite.com/bad.html');return document.MM_returnValue"

-----

Now, if you want to redirect purely on browsers, and make IE go to good.html and NS go to bad.html:

&lt;script language="JavaScript"&gt;
<!--
function MM_checkBrowser(NSvers,NSpass,NSnoPass,IEvers,IEpass,IEnoPass,OBpass,URL,altURL) { //v3.0
  var newURL='', verStr=navigator.appVersion, app=navigator.appName, version = parseFloat(verStr);
  if (app.indexOf('Netscape') != -1) {
    if (version >= NSvers) {if (NSpass>0) newURL=(NSpass==1)?URL:altURL;}
    else {if (NSnoPass>0) newURL=(NSnoPass==1)?URL:altURL;}
  } else if (app.indexOf('Microsoft') != -1) {
    if (version >= IEvers || verStr.indexOf(IEvers) != -1)
     {if (IEpass>0) newURL=(IEpass==1)?URL:altURL;}
    else {if (IEnoPass>0) newURL=(IEnoPass==1)?URL:altURL;}
  } else if (OBpass>0) newURL=(OBpass==1)?URL:altURL;
  if (newURL) { window.location=unescape(newURL); document.MM_returnValue=false; }
}
//-->
&lt;/script&gt;
'

And in the body section: onLoad="MM_checkBrowser(4.0,2,2,4.0,1,1,2,'http://www.yoursite.com/good.html','http://www.yoursite.com/bad.html');return document.MM_returnValue"

BTW- These scripts were generated for me in about 10 seconds using the Dreamweaver behaviors function... You might consider purchasing that program, as it makes things like this really easy.

They have: 40 posts

Joined: Jul 2001

Thanks Brian -

I actually do own DW 4 and am currently using it. That's great that the behaviors pannel can do that - I'll go check it out!

What should the operation be to set the behavior going - I would think "onload" but that option is not available????

Thanks!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.