Writing Conditional Code for Internet Explorer In a Javascript File

I would like to know if it is possible to write conditional javascript in a javascript file for Internet Explorer.

i.e. something like that...

if (is IE7) { do this } else { do this instead }); 

I know that I can load a completely different script for IE using conditional comments in my head, but I only want to change a small piece of code, and therefore loading a completely different sheet will be the "expensive" way that.

+3
source share
7 answers

If you use jquery, you do it

 if ($.browser.msie && $.browser.version == '6.0') { //do IE specific code } 
0
source

When writing Javascript , a browser discovery method is always used to define a function . Therefore, instead of if (IE7) do if (feature) .

For example, if you want to find out if your browser supports getElementsByClassName() instead of checking the browser version, you check for the presence of a function ( if (document.getElementsByClassName) ).

Read this great article:

Object Detection on Quirksmode

If you want to find out the browser that supports your page with some of the objects that you want to use in your code, you should never use browser detection. Of course, you know that this-and-browser will support your code while such and such a browser is a habit. But what about other browsers, obscure browsers?

+4
source

Not inside the JavaScript file directly.

Several alternatives:

Use a global variable before loading the script to validate your JavaScript file. This is a bit of a hybrid approach and may get messy, but it guaranteed IE detection.

 <!--[if IE]> <script type="text/javascript"> var is_ie = true; </script> <![endif]--> <script type="text/javascript" src="somefile.js"></script> 

Or a more traditional approach using a browser or detecting objects in a JavaScript file.

+2
source

Conditional compilation is exactly what you are looking for.

 <script> /*@cc_on @if (@_jscript_version == 5.7 && window.XMLHttpRequest) document.write("You are using IE7"); @end @*/ </script> 
+2
source

Although this is the answer to the original question, this is NOT what you should do. So don’t do it!

Why not work out which browser you use and save it in a variable in javascript. Then you can have if statemenets and the like in your javascript. For example, if I am IE, then do it, otherwise do it. You get the idea!

Did you see that? Browser charge

Significant Bit:

 var is = { ff: window.globalStorage, ie: document.all && !window.opera, ie6: !window.XMLHttpRequest, ie7: document.all && window.XMLHttpRequest && !XDomainRequest && !window.opera, ie8: document.documentMode==8, opera: Boolean(window.opera), chrome: Boolean(window.chrome), safari: window.getComputedStyle && !window.globalStorage && !window.opera } 
+1
source

My transition to a script for this is the PPK BrowserDetect script . It is lightweight, easy to understand and does not require the use of a library. When it loads, you can write code, for example:

 if (BrowserDetect.browser == "Explorer" && BrowserDetect.version >= 6 && BrowserDetect.version <= 8) { // IE6-8 code { 

Of course, you should avoid using this at all (reasonable) costs, but where it’s cleaner, to quarantine IE-specific code, and not try to crack IE functions and errors.

+1
source

If you want to use jquery, it has a built-in browser.

http://api.jquery.com/jQuery.browser/

-one
source

All Articles