Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you disable <script> elements using JavaScript

Tags:

javascript

dom

I want to disable <script> tags. Here's my code, but it doesn't work.

document.getElementsByTagName('script').disabled = true;
like image 662
vurquee Avatar asked Mar 24 '12 08:03

vurquee


People also ask

How do I stop a script from a website?

In the Internet Options window, click the Security tab. In the Security tab, click Custom Level button. Scroll down the list (close to the bottom) and locate Active Scripting. Select Disable, Enable, or Prompt to adjust your JavaScript settings.

How to stop JavaScript running on a page?

Press Ctrl + Shift + P (Windows, Linux) or Command + Shift + P (macOS) to open the Command Menu. Start typing javascript , select Disable JavaScript, and then press Enter to run the command. JavaScript is now disabled.

How do I remove the DOM script element?

We can remove a script from the DOM by scanning through all scripts on the page, getting the parent node of that script, and then finally removing the child of that parent node.


3 Answers

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/

like image 195
Denis Chmel Avatar answered Sep 29 '22 14:09

Denis Chmel


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.

like image 29
Eugene Avatar answered Sep 29 '22 14:09

Eugene


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);
like image 44
Jeff Luyet Avatar answered Sep 29 '22 13:09

Jeff Luyet