How do you disable – Felix Kling Mar 24 '12 at 09:00

  • @FelixKling : I will keep that in mind, Sir. I'm a JS noob. I still haven't really got the hang of it. Thanks for the tip! +1 for the specs. – vurquee Mar 24 '12 at 09:05
  • 6 Answers6

    17

    In fact, it is possible to disable execution by changing "type" attribute:

    <script type="text/javascript">
        alert("I will alert you");
    </script>
    
    <script type="application/json">
        alert("And I will keep silent");
    </script>
    
    <script>
        alert("I will alert too");
    </script>
    

    http://jsfiddle.net/r6c0x0sc/

    Denis Chmel
    • 780
    • 6
    • 11
    • 1
      But if it's executed it won't be executed again anyway. So no need to disable it in this case. If it has added an event listener, or you did setInterval() in it - surely those won't be disabled even if you delete – Denis Chmel May 09 '17 at 13:55
    • This seems quite useful for user generated or embedded HTML that is rendered and displayed after the main page load. – The Thirsty Ape Nov 04 '22 at 14:08
    12

    Can't be done... A script tag evaluates as soon as the DOM renderer renders it, so getting a handle on it after wards won't do much.

    Eugene
    • 2,946
    • 2
    • 26
    • 30
    1

    You can disable a script element. There are a couple of ways:

    You can specify a different script type as other have listed. Heres the one that worked for me:

    //loop through all script tags on page
    $('script').each(function(){
        var scripthtml = $(this).html();
        $(this).replaceWith('<script type="text/gzip">' + scripthtml + '</script>');
    });
    

    All of the official script types can all be found here: iana.org

    The second way is just a simple if statement:

    //loop through all script tags on page
    $('script').each(function(){
        var scripthtml = $(this).html();
        $(this).replaceWith('<script>if (1==0){' + scripthtml + '}</script>');
    });
    

    The if statement will always be false, so anything inside the script tag won't execute. However all of your functions() inside the script tag will all be valid.

    Heres the javascript equivalent of replaceWith:

    //get script and html
    var yourscripttag = document.getElementById('yourscripttagID');
    var scripthtml = 'if (1==0){' + yourscripttag.innerHTML + '}';
    //remove script
    yourscripttag.remove();
    //create new script element
    var newscript=document.createElement('script');
    newscript.type='text/javascript';
    //insert html in new script tag
    newscript.appendChild(document.createTextNode(scripthtml));
    //insert new script tag into head
    document.getElementsByTagName('head').item(0).appendChild(newscript);
    
    Jeff Luyet
    • 424
    • 3
    • 10
    1

    You can hack it. Use another script to do a....

    node.parentNode.replaceChild(
      document.createComment(node.outerHTML.slice(1,-1)
        .replace(/--/g, '\\-\\-')), node);
    

    Where node is your script node/element. The replacing of double dashes is to prevent the browser complaining about an uncompliant comment tag.

    Then if you want to enable it just do the reverse comment.replace and enclose with < and >. But you probably need to keep track of the modified script node, because after it becomes a comment it is less query-able.

    Ismael Harun
    • 143
    • 2
    • 7
    0

    As I understand you are getting some HTML code from the Database and add it to the page. If so, You can write a Server Side function that will add a HTML Notes around the scripts using a simple Regex and after that the user saving the changes you will need to remove the notes.

    You can do it also in Javascript if you have the HTML string that you like to add before you add it to the Dom.

    Roy Shoa
    • 3,117
    • 1
    • 37
    • 41
    -1

    Removing all the events on DOM nodes will prevent most of the JavaScript from executing (here for document):

    for (key in getEventListeners(document)) {
      getEventListeners(document)[key].forEach(function(c) {
        c.remove()
      })   
    }
    
    Dorian
    • 22,759
    • 8
    • 120
    • 116