L84
L84

Reputation: 46298

Run a Script to Disable a Script

I need to run a script that will disable a another script on the page. How do I do this?

(Internet Explorer Issues is why I am having to do this.) I am using jQuery 1.8.

Please provide examples.

Upvotes: 1

Views: 176

Answers (3)

Oliver Moran
Oliver Moran

Reputation: 5157

A "nicer" way to conditionally attach scripts depending on whether a browser is IE or not is to use "conditional comments". These are available in IE5+ but are compatible with all browsers because of how they are designed. They allow you to set certain parts of your HTML as being intended for for IE only, or for all non-IE browsers, or all browsers (IE and non-IE).

Example:

<!--[if IE 8]>
<script src="internet-explorer.js"></script>
<![endif]-->

<![if !IE]>
<script src="not-internet-explorer.js"></script>
<![endif]>

<script src="all-browsers.js"></script>

Link: http://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx

Upvotes: 0

Gil Zumbrunnen
Gil Zumbrunnen

Reputation: 1062

Using jQuery you can test for IE and then load the script if it returns true.

jQuery.browser might be moved to a plugin later, if it breaks when you update your library start looking into it first.

Keep in mind that it is usually not advisable to test for a specific browser, better to test for features the browser can work with. Sometimes you don't have a choice:

if(jQuery.browser.msie)
{
    $.getScript("lynda-ie.js");
}

If you need a script for a specific version if IE you can also test for its version :

if(jQuery.browser.msie)
{
    switch (parseInt(jQuery.browser.version,10))
    {
        case 6 :
            $.getScript("lynda-ie-6.js");
        break;
        case 7 :
            $.getScript("lynda-ie-7.js");
        break;
        case 8 : case 9 : default :
            $.getScript("lynda-ie.js");
        break;
}

Upvotes: 3

Royi Namir
Royi Namir

Reputation: 148514

Why don't you use a flag ?

If this flag is "1" ( for example) the code will run

otherwise , it won't.

Upvotes: 0

Related Questions