1

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.

L84
  • 45,514
  • 58
  • 177
  • 257
  • you can remove the script tag of the script you dont want to run by javascript – NullPoiиteя Aug 19 '12 at 08:53
  • 2
    What have you tried so far? What are the Internet Explorer issues you are encountering? Are these scripts all on a page you have created yourself? – Richard Ev Aug 19 '12 at 08:55
  • @RichardEv - Nothing because I do not know where to start and JS is not my strong suit. I attempted to use conditional comments for my issue but those are not working correctly. – L84 Aug 19 '12 at 08:57
  • If you show us the conditional comments approach you have tried, it will tell us a little more about the problem you are having. This will hopefully help us help you. – Richard Ev Aug 19 '12 at 08:59
  • **@Lynda** I think you might be complicating things too much. I answered [your other question](http://stackoverflow.com/questions/12025232/targeting-ie-8-and-below). I believe that's what you need to fix IE. Try to work with that, you don't need multiple scripts if you do it that way. Like I showed you can just use `if (isIE8)` within your code when you need it. – elclanrs Aug 19 '12 at 09:00
  • @elclanrs - There is an issue with the way a script is handled by IE. Only way i know around this error is to run two separate scripts, one for FF, Chrome, etc and the other for IE 8 and below. – L84 Aug 19 '12 at 09:05
  • You can just ignore IE like this `if (!isIE8 || !isIE7) { ... }` and that code will run on every other browser that's not IE8 or 7. To _"Run a Script to Disable a Script"_ is not a great solution, there must be a better way to do it. – elclanrs Aug 19 '12 at 09:09
  • @elclanrs - I agree, there must be a better way. I despise IE... – L84 Aug 19 '12 at 09:10

3 Answers3

3

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;
}
Gil Zumbrunnen
  • 1,062
  • 6
  • 17
0

Why don't you use a flag ?

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

otherwise , it won't.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
0

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

Oliver Moran
  • 5,137
  • 4
  • 31
  • 45
  • That is what I originally attempted but for some reason it was not working correctly. – L84 Aug 19 '12 at 18:14