4

I have a number of tags on my page.

<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="script3.js"></script>
<script type="text/javascript" src="script4.js"></script>
<script type="text/javascript" src="script5.js"></script>
<script type="text/javascript" src="toRemove.js"></script>

I've tried:

html.replace(/<script type="text/javascript" src="toRemove.js">.*<\/script>/ims, " ");
9
  • 4
    Scripts can't be removed from the page. You can remove a script element, but the program remains. Commented Oct 30, 2014 at 11:48
  • What is your purpose of deleting the script tag? Commented Oct 30, 2014 at 11:49
  • 1
    You need to escape the slash: /<script type="text\/javascript.../ Commented Oct 30, 2014 at 11:49
  • @A1rPun - this script is very heavy on page load, so when i open a new window (which is a copy of the current) it carrys out extra processes and there is no need for this tag. Commented Oct 30, 2014 at 11:50
  • 1
    You could load your page without the scripts , and then add only the necessary scripts one by one with something like (function() { var sc = document.createElement('script'); sc.type = 'text/javascript'; sc.src = 'foo.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(sc, s); })(); Commented Oct 30, 2014 at 11:56

2 Answers 2

1

As other people already stated, you cannot "unload" a script by removing its node. However, technically you can of course remove the script node. I would not recommend using a RegEx on the documents HTML string, but rather

[].forEach.call(document.head.getElementsByTagName('script'), function( scr ) {
  if( scr.getAttribute( 'src' ) === 'toRemove.js' ) {
      scr.parentNode.removeChild( scr );
  }
});

Ideally, you're not working in your life document there, but rather create a clone via createDocumentFragment for instance, which you can then re-use for whatever reason.

Sign up to request clarification or add additional context in comments.

5 Comments

Why not to use scripts collection, OP didn't say if these scripts are within head or body.
Why not document.querySelectorAll('script[src=toRemove.js]')? Should work even for IE8.
Y U NO ! Well, this is just an example, of course you can use other query methods.
@YuryTarabanko - IE8 support is a must :(
@OamPsy Well, querySelectorAll is not your problem :). Array.prototype.forEach is. :)
1

You can try querySelectorAll which will fetch script tag from body as well as head.

var scriptTagElements=document.querySelectorAll('script[src=toRemove.js]')

for(var i=0;i<scriptTagElements;i++){

    scriptTagElements[i].parentNode.removeChild(scriptTagElements[i]);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.