Hi I want a code that can detect the browser. all i want it to do is something like: if IE7 { something} else { something } Any one know how i can do this.
I've not used myself, but I've found several pages about this: - http://www.maratz.com/blog/archives/2006/10/31/reliable-ie-7-detection-with-javascript/ - http://ajaxian.com/archives/detecting-ie7-in-javascript And the best, seems this: - http://parentnode.org/javascript/javascript-browser-detection-revisited/
Well, that's the easy part: <html> <head> <script type="text/javascript"> var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false; if (ie7==true) { document.write( "Is IE7" ); } else { document.write( "Is NOT IE7" ); } </script> </head> <body> </body> </html> Code (markup): Of course, you can use the other methods explained in the above pages to set the ie7 variable .
I would suggest you to not go for browser detection. Instead use object detection. That will be much more reliable.
I believe ajsa52 is using object detection in his code above, but here is a guide to it: http://javascriptkit.com/javatutors/objdetect3.shtml
Object detection means checking for the existence of a method or property rather than checking for the browser. For example take example of httprequest for ajax. In browser detection, you try to find out which is the browser and then create the httprequest accordingly (ie use new XMLHttpRequest() for Fx or new ActiveXObject('MSXML2.XMLHTTP') for IE). But in object detection you check for the availability of XMLHttpRequest and if it is available we create an object of it, else we try for the other. eg: if(window.XMLHttpRequest) { req = new XMLHttpRequest(); }else{ try{ req = new ActiveXObject('MSXML2.XMLHTTP'); }catch(e){ try { req = new ActiveXObject('Microsoft.XMLHTTP'); }catch(e){ return false; } } } Code (markup):