Reputation: 65
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
}
}
Upvotes: 0
Views: 2984
Reputation: 13928
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.
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.
You check if the content of our Trojan div is changed, if yes then remove it from the Dom.
Now execute your script.
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(){});
Upvotes: 0
Reputation: 10216
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
Upvotes: 1