View Full Version : Alternative browser redirect script -- is this usable
bradyj
05-14-2003, 06:29 PM
Back to the browser redirect issue:
I was given an adjustment to the dynamic drive script that redirects browsers for degrading purposes (this is not an arguement as to if this is right or wrong, we are not going to use it for making 'lite' sites at my company, just understanding the script), and I'm wondering if this is actually useful to read all types of moder browsers:
var browser_type=navigator.appName
var browser_version=parseInt(navigator.appVersion)
if (!document.getElementById)
window.location.replace("http://www.blahblah.com")
Would this actually read only modern browsers properly? Or will it have issues with various browsers, as it doesn't specifically list them by name & version.
sage45
05-14-2003, 07:21 PM
Well here would be how it would work...
var browser_type=navigator.appName
This line asks for the browser name (Netscape Navigator, Internet Explorer, etc. etc.) and sets it to equal a variable, in this case "browser_type"
var browser_version=parseInt(navigator.appVersion)
This line asks for the browser version (6.0, 5.0, etc. etc.) and sets it to equal a variable, in this case "browser_version"
Now the first two by themselves are just collecting information, but this next one actually does something
if (!document.getElementById)
First it checks to see if document.getElementById returns as a value or not (this is parsed through the browser, if the browser understands the DOM element it returns element Id's, if the browser does not understand the DOM elelment, it doesn't return anything)... With that in mind, in this line you are checking to see if this is an earlier version browser or one that does not understand the DOM element of getElementById... This is accomplished by the use of the "!" or not... So the line would be read as:
If not document.getElementById
So if no value is returned then the window.location.replace executes... However, if a value comes back then the script skips the window.location.replace...
window.location.replace("http://www.blahblah.com")
Your script is ofcourse incomplete and also wouldnt work as a couple of nice little guys were forgotten...
<SCRIPT LANGUAGE="JavaScript">
<!--
var browser_type=navigator.appName
var browser_version=parseInt(navigator.appVersion)
if (!document.getElementById) {
window.location.replace("http://www.blahblah.com")
}
After this point by knowing the browser name and version you can do other things like:
if ((browser_type == "Microsoft Internet Explorer") && (browser_version >= 4 )) {
do some stuff
}
if ((browser_type == "Netscape") && (browser_version >= 3 )) {
do some stuff
}
//-->
</SCRIPT>
So forth and so on...
Now all that being said, would it work, yes... Would it work in all browsers, well yes becuase the ones that did not understand the line would return nothing at all... Your using browser elements to verify version and type... This way you don't have to know the name or version of the browser... When it comes down to it, it just depends on how specific you want to design your pages for all browsers...
HTH,
-sage-
bradyj
05-14-2003, 08:53 PM
You... are a god.
That was a great explanation:thumbsup: , and probably one of the first times in the past two weeks that I've been trying to use javascript that it actually clicked in my head -- which is finally what I needed, it's starting to look like an understandable language; up there with spanish and HTML:)
Excellent -- I will read further and play to see what I can do with this... thank you again sage45!!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.