views:

198

answers:

4

is there a way to disable a certain script for all ie browsers?

+4  A: 

You can make use of conditional compilation to determine if the client is using MSIE.

var IE = /*@cc_on!@*/false;

which can be used as

if (IE) {
    // IE.
} else {
    // Others.
}

Only in IE, the ! will be compiled and taken in the expression, resulting in a new expression !false, which is logically true. This works better than $.browser.msie because it can be fooled by the useragent and also better than document.all because it would affect certain Opera versions as well.

That said, what is it you're trying to disable? You can on the other hand also make use of feature detection. Here's a discussion about this: http://stackoverflow.com/questions/1294586/browser-detection-versus-feature-detection

BalusC
i am just trying to disable a jquery fadein effect only on ie since it's too heavy on it. thank you for the details, this is exactly what i wanted to do.
+1  A: 

I wouldn't recommend this, but:

if(!$.browser.msie) {
  //non IE script
}

I would fix the script to work in IE, or exclude it based on some feature the browser doesn't support...not just because it's IE. With any browser a feature could be added via an update tomorrow, and your script would still exclude it. See $.support for more on feature detection.

Excluding something from running because "it isn't supported" is a perfectly valid scenario. However, excluding something because "IE doesn't support it...when I wrote this code" isn't a good approach. Instead, check if the feature that you need is present, and the user gets the richest experience possible in their current browser.

Nick Craver
A: 

If you're speaking of IE 6, you can crash it by calling this function :

function crash_IE6() {for(x in document.open);}

Seriously, the most use way of deteting IE is checking the presence of document.all... but it still isn't a good thing. You should nerver check what browser your script is running on... you should just check the presence of the needed methods.

xavierm02
Could the people who voted down explain why ? :-°
xavierm02
I didn't down vote but I would expect others did because the first line in your answer doesn't relate to the question at all. Also you say "You should nerver check what browser your script is running on". I think most people would disagree with you on this. There are many cases where you would need to know what browser you are running.
Rob Segal
I nerver said knowing what browser you're running on was a bad thing...But this information may be wrong... because the client may want to hide it in order to make exploits try fail or whatever... but still you can't be sure your detection if right...And it isn't future-proof at all. Browsers will change and you'll to update your detection so that it detects all browsers. Feathures detection is future-proof and allows you to keep the same code forever (with some minor changes).
xavierm02
+1  A: 

You could not include the javascript at all for IE browsers using Microsoft's recommended way of inserting a conditional comment:

<!--[if !IE]>
<script src="myscript.js" type="text/javascript"></script>
<![endif]-->

or simply wrap the code you want to exclude in the comment.

Dan Diplo
That isn't the syntax for a "not IE" conditional comment.
David Dorward
According to the Microsoft site I linked to it is. If you think you know better than please post what you think is correct, otherwise retract your comment.
Dan Diplo