Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Conditional Code for Internet Explorer WITHIN a Javascript file

I'd like to know if it's possible to write conditional javascript within a javascript file for Internet Explorer.

i.e. something like this...

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

I know I can load a completely different script for IE using conditional comments in the head, but I only want to change a small piece of code and so loading a completely different sheet would be an 'expensive' way to do that.

like image 664
SparrwHawk Avatar asked Dec 04 '25 22:12

SparrwHawk


1 Answers

When writing Javascript, doing feature detection is always the way to go instead of browser detection. So instead of doing if (IE7) do if (feature).

For example, if you want to know if your browser supports getElementsByClassName(), instead of checking the browser version, you check for the existence of the function ( if (document.getElementsByClassName) ).

Please read this great article:

Object detection on Quirksmode

If you want to know whether the browser that views your page supports certain objects you want to use in your code, you should never EVER use a browser detect. Sure, you know that this–and–that browser will support your code while such–and–so browser won’t. But how about other browsers, obscure browsers?

like image 87
kapa Avatar answered Dec 06 '25 13:12

kapa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!