Browser Detect
-
I've never done javascript before but I need to be able to detect which browser the user is running. I've searched the net and copied some code to do this. It's simple and I understand what it does but I can't get the .html page to display different text per browser. This is what I have in my html:
<!-- if(BrowserDetect()!="Internet Explorer") { document.write('Other'); } else { document.write('Internet Explorer'); } // -->
and this is the code that I found on the internet:function BrowserDetect() { var detect = navigator.userAgent.toLowerCase(); var OS,browser,version,total,thestring; if (checkIt('konqueror')) { browser = "Konqueror"; OS = "Linux"; } else if (checkIt('safari')) browser = "Safari" else if (checkIt('omniweb')) browser = "OmniWeb" else if (checkIt('opera')) browser = "Opera" else if (checkIt('webtv')) browser = "WebTV"; else if (checkIt('icab')) browser = "iCab" else if (checkIt('msie')) browser = "Internet Explorer" else if (!checkIt('compatible')) { browser = "Compatible" version = detect.charAt(8); } else browser = "An unknown browser"; if (!version) version = detect.charAt(place + thestring.length); if (!OS) { if (checkIt('linux')) OS = "Linux"; else if (checkIt('x11')) OS = "Unix"; else if (checkIt('mac')) OS = "Mac" else if (checkIt('win')) OS = "Windows" else OS = "an unknown operating system"; } document.rs_browser=browser; document.rs_OS=OS; document.rs_version=version; return browser; } function checkIt(string) { place = detect.indexOf(string) + 1; thestring = string; return place; }
When I view my page in either FireFox or IE it doesn't generate any errors but it also doesn't display any sort of text. Please Help, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't. -
I've never done javascript before but I need to be able to detect which browser the user is running. I've searched the net and copied some code to do this. It's simple and I understand what it does but I can't get the .html page to display different text per browser. This is what I have in my html:
<!-- if(BrowserDetect()!="Internet Explorer") { document.write('Other'); } else { document.write('Internet Explorer'); } // -->
and this is the code that I found on the internet:function BrowserDetect() { var detect = navigator.userAgent.toLowerCase(); var OS,browser,version,total,thestring; if (checkIt('konqueror')) { browser = "Konqueror"; OS = "Linux"; } else if (checkIt('safari')) browser = "Safari" else if (checkIt('omniweb')) browser = "OmniWeb" else if (checkIt('opera')) browser = "Opera" else if (checkIt('webtv')) browser = "WebTV"; else if (checkIt('icab')) browser = "iCab" else if (checkIt('msie')) browser = "Internet Explorer" else if (!checkIt('compatible')) { browser = "Compatible" version = detect.charAt(8); } else browser = "An unknown browser"; if (!version) version = detect.charAt(place + thestring.length); if (!OS) { if (checkIt('linux')) OS = "Linux"; else if (checkIt('x11')) OS = "Unix"; else if (checkIt('mac')) OS = "Mac" else if (checkIt('win')) OS = "Windows" else OS = "an unknown operating system"; } document.rs_browser=browser; document.rs_OS=OS; document.rs_version=version; return browser; } function checkIt(string) { place = detect.indexOf(string) + 1; thestring = string; return place; }
When I view my page in either FireFox or IE it doesn't generate any errors but it also doesn't display any sort of text. Please Help, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.Try moving the first two lines of the BrowserDetect function - var detect = navigator.userAgent.toLowerCase(); var OS,browser,version,total,thestring; - (ie the variable declarations) outside of the function - they are global variables (at least 'detect' and 'thestring' are also used in the checkIt function, so...) cheers Phil
-
Try moving the first two lines of the BrowserDetect function - var detect = navigator.userAgent.toLowerCase(); var OS,browser,version,total,thestring; - (ie the variable declarations) outside of the function - they are global variables (at least 'detect' and 'thestring' are also used in the checkIt function, so...) cheers Phil
That worked. Thanks! I guess I should keep an eye on what scope the variables are in. Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.