PDA

View Full Version : Browser Redirection Based on Browser Version



nathaliaMota
06-09-2005, 12:27 AM
I can't figure out why the following code does not work. It's supposed to open a certain frame if a user is using a browser version 3 or higher, but it doesn't work. Can somebody show me a sample of some code that would do this, and actually work?

Thank you

<html>
<head>
<title>
Rescue Africa
</title>
</head>
<!-- This doesn't work at all. Why? See page 124 and 125 of HTML artistry-->
<script language="JavaScript">
var ver = navigator.appVersion;
document.write('<frameset frameborder="0" border="0" cols="468, 424, 114,*"> ');
document.write('<frameset frameborder="0" border="0" rows="232,30,253,*"> ');
if (ver >= 3){
document.write('<frame name="nav" src="topFace2.html" scrolling="no" marginwidth="0" /> ');
}
//This part of the script does not appear to be working. It's opening topFace1.html instead of
//topFace2.html on IE 6.0
else{
document.write('<frame name="nav" src="topFace1.html" scrolling="no" marginwidth="0" /> ');
}
document.write('<frame name="subNav" src="crisis.html" scrolling="no" marginwidth="0" /> ');
document.write('<frame name="bottom" src="bottomFace.html" scrolling="no" marginwidth="0" /> ');
document.write('<frame name="blackRow" src="black.html" scrolling="no" marginwidth="0" /> ');
document.write('</frameset> ');
if (ver >= 3){
document.write('<frame name="middle" src="crisisTXT.html" scrolling="no" marginwidth="0" /> ');
}
//This part of the script does not appear to be working. It's opening crisisTXTOldB.html instead of
//crisisTXT.html on IE 6.0
else{
document.write('<frame name="middle" src="crisisTXTOldB.html" scrolling="no" marginwidth="0" /> ');
}

document.write('<frame name="right" src="right.html" scrolling="no" marginwidth="0" />');
document.write('<frame name="blackCol" src="black.html" scrolling="no" marginwidth="0" />');
document.write('</frameset>');
</script>


<noframes>
<body>
Your browser does not allow frames.
</body>
</noframes>

</html>

jscheuer1
06-09-2005, 03:50 AM
'navigator.appVersion' returns this:

4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705)

and similar type strings when queried in javascript. It is never a number in the strict sense of the term. You have set the varaiable 'ver' to this string then you are doing a math operation on it:
var ver = navigator.appVersion;
....document.write stuff was here....
if (ver >= 3)ver will never be greater than nor equal to 3. You might try this:
var ver = parseInt(navigator.appVersion);Using the navigator.appVersion string:

4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705)

this will set ver to 4.

mwinter
06-09-2005, 09:58 AM
It's supposed to open a certain frame if a user is using a browser version 3 or higherOf the 'standard' browsers, all of them are beyond version 3. You won't be encountering NN3 or IE3, for example. You may not even encounter IE4 or NN4, so dump that junk.

There's another reason to dump it: not all user agents follow Microsoft's version numbering. I believe the most recent version of Safari is 1.2, and Opera is at 8.0. I can't check what Safari reports as its version number, but I know Opera returns the true number, not an equivalent.

The final reason is that not everyone enables client-side scripting. Basing a frameset document on a script will render the entire site useless. Simply telling someone that they don't support frames is equally dumb (for both users and search engines).

Browser detection is flawed and almost always, useless. The far more useful information is what a browser can do. Read the browser detection (http://www.jibbering.com/faq/#FAQ4_26) entry in the comp.lang.javascript FAQ (including the link to the notes for more information).



var ver = parseInt(navigator.appVersion);Unless one has a very specific reason, the parseInt function should never be called without the radix argument.

Mike