1

So one of the scripts I have in my website is a mouse hover script that changes color and image. However this is not behaving properly in IE so I would like to disable that script if the user is using IE.

How can i do that?

Example of what I want:

//if IE not detected(any other browser detected)
     <script type="text/javascript" src="js/mousehover.js"></script>

//else (IE detected)
     //do nothing / don't upload the script
2

2 Answers 2

2

It's probably better to debug the script, at least for IE8+.

If you really want to avoid loading a script on IE, though, I believe it's the only browser with ActiveXObject, so the simplest thing is probably just to put a guard around your code in mousehover.js:

if (typeof ActiveXObject !== "undefined") {
    // IE, don't do the mouse hover stuff
}

Or if it's really important to you not to download that JS on IE, you can do that in two ways:

<script>
(function() {
    if (typeof ActiveXObject === "undefined") {
        var s = document.createElement('script');
        s.src = "js/mousehover.js";
        document.documentElement.appendChild(s);
    }
})();
</script>

That will load the script only on non-IE. But note that any subsequent scripts you have will not wait for that script to load, so if there are dependencies, you'll need to watch for them.

or using document.write:

<script>
if (typeof ActiveXObject === "undefined") {
    document.write('<scr' + 'ipt src="js/mousehover.js"></scr' + 'ipt>');
}
</script>

...which will maintain load order, but can't be used in XHTML.

Sign up to request clarification or add additional context in comments.

4 Comments

Why do you suggest script solutions (last two) over conditional comments? Conditional comments are more solid, because they cannot be spoofed. And it's more readable than the last two options you've presented, imo.
@RobW: Does IE10 do away with ActiveXObject? I was trying to handle all versions of IE.
@RobW: Wow, they're really getting with the program! :-)
@Justin: Edge is not IE.
0

You can use conditional comments:

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

6 Comments

Not as of IE10 (FWIW).
This is a wrong implementation of non-IE CC. You need to append <!--> at the top and prefix <!-- at the bottom. Furthermore, this will only affect IE 9 and lower, not IE10+.
So, what conditional comment could I use to work with all IE?
@DaftDev Which IE versions have you tested? It's very likely that the script works fine in recent versions of IE (9, 10, 11). Can you edit your question to include mousehover.js and explain what's "not working", if it has a reasonable size?
@DaftDev: At this stage, what Rob's suggesting probably warrants a separate question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.