0

I have a web page which is working fine on my local machine, but when it is moved to the server, an ad script from google overwrites page image src. I want to find that particular script tag with that src and remove it. I know the url, but can not add an id or modify the DOM. Please help.

for eg:

<script src="one.js"><script>
<script src="two.js"><script>
<script src="three.js"><script>

so if

var scripts = document.getElementsByTagName ( "script" );
var l = scripts.length;
for ( var i = 0; i < l; ++ i ) {
    if ( scripts[i].src ="one.js") {
        //remove that particular script tag with one.js
    }
}
5
  • You can do it by : scripts[i].parentNode.removeChild(scripts[i]) Commented Nov 2, 2015 at 13:52
  • 6
    This may not do you any good - removing the script tag will have no effect once the script within has already loaded and run. Commented Nov 2, 2015 at 13:56
  • 1
    Instead of looping, you can use document.querySelector("script[src='one.js']") @PaulRoub, Your point is valid! Commented Nov 2, 2015 at 13:59
  • You could maybe catch it before it has loaded with MutationObserver but the best is probably to get what the script does, and undo it after him. Commented Nov 2, 2015 at 14:02
  • hmm is there any alternative by which we can stop loading that particular script ?? Commented Nov 2, 2015 at 17:21

2 Answers 2

1

As Paul stated in the comments, removing an already loaded script wont do any change. But, for the sake of it:

$('script[src*="one.js"]').remove();

with jQuery (as this question is tagged).

Edit: for more info about attribute selectors (like *="string") have read here

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

7 Comments

What is the use of * in selector ? pls explain!
@RayonDabre it means select any <script> tag which's src attribute somewhere has the string one.js in it, while the position doesnt matter. ^= means has to start with, $= means has to end with
I would suggest you to provide this info in your answer as it sounds very helpful!
@RayonDabre it is common knowledge, have a read here: api.jquery.com/category/selectors/attribute-selectors
@Kaiido you cant compare selectors with methods :)
|
0

Not quite an answer per say, but still i have a gut feeling that i should post this workaround.

So if I were you, maybe I'd try to trap the script by creating the fake set of elements that the script is meddling with and then verify if the respective data has changed ad then delete that div.

Better explained step by step.

  1. Introduce a fake div with similar Dom before our concerned div. Now if there are two divs with same Id then that script shall target the first div with that id.

  2. You check if the content of our Trojan div is changed, if yes then remove it from the Dom.

  3. Now execute your script.

  4. Also as a counter measure just remove all the events that are delegated to that div. If the rogue script is jquery based, even better, since you can remove the respective event and bind your own, using the .off() function. As shown below..

    $("#xyz").off().on('click', function(){});

1 Comment

I am on my mobile at the moment, so if any formatting is wrong/missing plz ignore it, or even better fix it. ;)

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.