September 1, 200818 yr hi i would like to know how i'd implement a browser detection on my site using javascript? i find it really hard to understand JS so i need someone to explain it to me, basically i want to detect if the user is using IE6 or below and then return a message saying something along the lines of "this webpage may not appear correctly please upgrade to IE7 at blahblahbla" would i be able to use this script for it? http://www.quirksmode.org/js/detect.html ? thanks in advance (and please not have a go at me for not just making my site compatible with IE6 i will get round to it eventually just want a quick fix)
September 1, 200818 yr I really advise against doing something like this. It's really bad practice for so many reasons. You're better off just showing us the page that isn't displaying properly in IE6, then we can help you fix it. Anyway... Copy this into the head section of you're webpage. <script type="text/javascript"> <!-- var BrowserDetect = { init: function () { this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version"; this.OS = this.searchString(this.dataOS) || "an unknown OS"; }, searchString: function (data) { for (var i=0;i<data.length;i++) { var dataString = data[i].string; var dataProp = data[i].prop; this.versionSearchString = data[i].versionSearch || data[i].identity; if (dataString) { if (dataString.indexOf(data[i].subString) != -1) return data[i].identity; } else if (dataProp) return data[i].identity; } }, searchVersion: function (dataString) { var index = dataString.indexOf(this.versionSearchString); if (index == -1) return; return parseFloat(dataString.substring(index+this.versionSearchString.length+1)); }, dataBrowser: [ { string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb" }, { string: navigator.vendor, subString: "Apple", identity: "Safari" }, { prop: window.opera, identity: "Opera" }, { string: navigator.vendor, subString: "iCab", identity: "iCab" }, { string: navigator.vendor, subString: "KDE", identity: "Konqueror" }, { string: navigator.userAgent, subString: "Firefox", identity: "Firefox" }, { string: navigator.vendor, subString: "Camino", identity: "Camino" }, { // for newer Netscapes (6+) string: navigator.userAgent, subString: "Netscape", identity: "Netscape" }, { string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE" }, { string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv" }, { // for older Netscapes (4-) string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla" } ], dataOS : [ { string: navigator.platform, subString: "Win", identity: "Windows" }, { string: navigator.platform, subString: "Mac", identity: "Mac" }, { string: navigator.platform, subString: "Linux", identity: "Linux" } ] }; BrowserDetect.init(); // --> </script> Stick this where ever you want it to be displayed in on the page (between the body tags). <script type="text/javascript"> <!-- if (BrowserDetect.browser+BrowserDetect.version!="Explorer7") { document.write('<p class="accent">You\'re using ' + BrowserDetect.browser + ' ' + BrowserDetect.version + '. Please use IE7!</p>'); } // --> </script> Fixed
September 1, 200818 yr Author I really advise against doing something like this. It's really bad practice for so many reasons. You're better off just showing us the page that isn't displaying properly in IE6, then we can help you fix it. Anyway... Copy this into the head section of you're webpage. <script type="text/javascript"> <!-- var BrowserDetect = { init: function () { this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version"; this.OS = this.searchString(this.dataOS) || "an unknown OS"; }, searchString: function (data) { for (var i=0;i<data.length;i++) { var dataString = data[i].string; var dataProp = data[i].prop; this.versionSearchString = data[i].versionSearch || data[i].identity; if (dataString) { if (dataString.indexOf(data[i].subString) != -1) return data[i].identity; } else if (dataProp) return data[i].identity; } }, searchVersion: function (dataString) { var index = dataString.indexOf(this.versionSearchString); if (index == -1) return; return parseFloat(dataString.substring(index+this.versionSearchString.length+1)); }, dataBrowser: [ { string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb" }, { string: navigator.vendor, subString: "Apple", identity: "Safari" }, { prop: window.opera, identity: "Opera" }, { string: navigator.vendor, subString: "iCab", identity: "iCab" }, { string: navigator.vendor, subString: "KDE", identity: "Konqueror" }, { string: navigator.userAgent, subString: "Firefox", identity: "Firefox" }, { string: navigator.vendor, subString: "Camino", identity: "Camino" }, { // for newer Netscapes (6+) string: navigator.userAgent, subString: "Netscape", identity: "Netscape" }, { string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE" }, { string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv" }, { // for older Netscapes (4-) string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla" } ], dataOS : [ { string: navigator.platform, subString: "Win", identity: "Windows" }, { string: navigator.platform, subString: "Mac", identity: "Mac" }, { string: navigator.platform, subString: "Linux", identity: "Linux" } ] }; BrowserDetect.init(); // --> </script> Stick this where ever you want it to be displayed in on the page (between the body tags). <script type="text/javascript"> <script type="text/javascript"> <!-- if (BrowserDetect.browser+BrowserDetect.version!="Explorer7") { document.write('<p class="accent">You\'re using ' + BrowserDetect.browser + ' ' + BrowserDetect.version + '. Please use IE7!</p>'); } // --> </script> thanks alot unfortently that code only brings up a syntax error :S and www.carl-robinson.co.uk/CHS there is PNG transparency issues im considering using a JS hack to sort that as well as some other problems. if you head to the site root (at the bottom of the page) it'll bring up the syntax error if your interested
September 1, 200818 yr On line 150 you've got <script type="text/javascript"> twice. EDIT: Oops i put that in my example, it's only meant to be there once!
September 1, 200818 yr Author On line 150 you've got <script type="text/javascript"> twice. EDIT: Oops i put that in my example, it's only meant to be there once! haha cheers i noticed that after myself! got it working but i've decided against it because to many people use bloody ie6 cheers anyways ive saved the script might come in useful
September 1, 200818 yr Even easier, something like this? <!--[if IE]> <p class="browseralert">You are using an inferior browser and as such your web experience will be diminished. Do yourself a favour and <a href="http://browsehappy.com/" title="Find out about proper web browsers at Browse Happy">get a better one now!</a></p> <link rel="stylesheet" type="text/css" href="styles/ie.css" media="screen" /> <![endif]-->
September 1, 200818 yr Yeah the if IE snippet posted above is a good way to go - you can define a specific stylesheet that will fix problems in IE. You could also have <!--[if lt IE7]> which means "if IE version is less than 7 - for example 6 or 5.5, use the following stylesheet...". As for your PNG transparency problem - this is a common one. But you should use transparent PNGs if you want. There are a number of ways to get it working in IE6. Perhaps the best is this gem: http://labs.unitinteractive.com/unitpngfix.php Really easy way to fix it - and it should validate.
Create an account or sign in to comment