I am trying to remove all script elements from a HTML page. But for some reason, I can only remove about half of them using the below:
function t(){
var r = document.getElementsByTagName('script');
for (var i = 0; i < r.length; i++) {
if(r[i].getAttribute('id') != 'a'){
r[i].parentNode.removeChild(r[i]);
}
}
}
I have that if condition so that I don't remove the executing script.
I am essentially trying to create a dynamic Javascript dis-abler for my selenium tests.
Dynamically removing an external JavaScript or CSS file To remove an external JavaScript or CSS file from a page, the key is to hunt them down first by traversing the DOM, then call DOM's removeChild() method to do the hit job.
Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.
The remove() method is used to remove an option from a drop-down list. Tip: To add an option to a drop-down list, use the add() method.
To remove a script from an applicationClick the Resources folder, right-click the script, and then click Remove.
Loop in reverse, the count is changing when you start removing nodes.
var r = document.getElementsByTagName('script');
for (var i = (r.length-1); i >= 0; i--) {
if(r[i].getAttribute('id') != 'a'){
r[i].parentNode.removeChild(r[i]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With