I would like to clear the entire head
section once the page loads... actually, my goal would be to delete all JavaScript code held in the head
section.
Is there some way to do something like this:
document.head.innerHTML = "";
Explanation:
I am using a Python script that uses Qt and webkit to take screenshots of websits.
It works on most sties, but there is one that it fails on. That site has a bunch of JavaScript code that it runs on timeouts. The WebKit webpage object allows me to execute JavaScript on the page. If there is some way to have the JavaScript remove all of the code form the head section I'd like to be able to try that for testing purposes to see if it resolves my screenshot script issue.
I would like to clear the entire head
section once the page loads... actually, my goal would be to delete all JavaScript code held in the head
section.
Is there some way to do something like this:
document.head.innerHTML = "";
Explanation:
I am using a Python script that uses Qt and webkit to take screenshots of websits.
It works on most sties, but there is one that it fails on. That site has a bunch of JavaScript code that it runs on timeouts. The WebKit webpage object allows me to execute JavaScript on the page. If there is some way to have the JavaScript remove all of the code form the head section I'd like to be able to try that for testing purposes to see if it resolves my screenshot script issue.
-
Have you actually tried
document.head.innerHTML = "";
? What happens? – Felix Kling Commented Mar 3, 2010 at 18:44 - one way it to remove the head using removeChild method and add it again which will be empty – Sarfraz Commented Mar 3, 2010 at 18:45
- 1 document.head.innerHTML = ""; did nothing when I tried it. – benino Commented Mar 3, 2010 at 19:09
5 Answers
Reset to default 3You can remove elements from the head, but it wont matter. The scripts have already run and removing the elements wont unload them or anything.
document.getElementsByTagName("head")[0].innerHTML = "";
// IE
var htmlEl = document.getElementsByTagName("html")[0];
htmlEl.removeChild(document.getElementsByTagName("head")[0])
var el = document.createElement("head");
htmlEl.appendChild(el);
To stop JavaScript scripts running by setTimeout, you should use clearTimeout, but for this case you should have a handler... But you're lucky, if they are defined as global variables.
Try using:
document.getElementsByTagName('head').innerHTML = "";
to clear the head. document.head does not exist so this may not work.
But you may want to try to disable the JavaScript using JavaScript like suggested by noah.
See http://www.manticmoo./articles/jeff/programming/javascript/removing-javascript-with-javascript.php for directions. It sounds crazy but evidently it works.
As noah said, simply removing the code from the head tag won't acplish anything. You have to actually undo what the code is doing. For example, if it's using setTimeout()
to set up the timeout code you're plaining about, you need to use clearTimeout()
to disable it.
$('#id').empty(); Worked very well.