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
  • 8 Answers8

    15

    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
    • 750
    • 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
    11

    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,849
    • 2
    • 24
    • 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
    • 346
    • 2
    • 9
    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
    • 2,967
    • 1
    • 34
    • 40
    0

    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
    • 20,859
    • 8
    • 116
    • 113
    0

    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
    • 113
    • 1
    • 7
    0

    Sorry, but you can't disable the <script> element.

    Dave
    • 57
    • 3
    -1

    Inline Java is a security risk. But this can be mitigated using a simple solution.

    Simply replace any instance of with something like <script&rt; so it does not render as a tag, but will output in HTML appearing as if it was a tag.

    input.replace("<script>", "&lt;script&gt;")
         .replace("</script>", "&lt;&#47;script&gt;")
    

    This will work if your use case is something like:

    1. If you want to disable inline script such as

      <script> alert("Hello"); </script>

    2. If you don't want to remove the content(script tag data) from display.
    Vaibhav Jain
    • 1,569
    • 21
    • 32
    • 1) Java and JavaScript are completely different. 2) The filtering code given here is trivially evaded - Case can be changed. Comments can be inserted. Spacing can be added. and many more. – ryanc Jun 27 '20 at 09:31