benino
benino

Reputation: 597

editing the <head> section with javascript

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.

Upvotes: 7

Views: 18526

Answers (5)

Nikhil Pingle
Nikhil Pingle

Reputation: 746

$('#id').empty(); Worked very well.

Upvotes: 0

Alex Ivasyuv
Alex Ivasyuv

Reputation: 8844

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.

Upvotes: 3

Chad Birch
Chad Birch

Reputation: 74558

As noah said, simply removing the code from the head tag won't accomplish 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 complaining about, you need to use clearTimeout() to disable it.

Upvotes: 0

Todd Moses
Todd Moses

Reputation: 11029

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.com/articles/jeff/programming/javascript/removing-javascript-with-javascript.php for directions. It sounds crazy but evidently it works.

Upvotes: 0

noah
noah

Reputation: 21519

You can remove elements from the head, but it wont matter. The scripts have already run and removing the elements wont unload them or anything.

Upvotes: 3

Related Questions