Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 840
benino Avatar asked Mar 03 '10 18:03

benino


People also ask

Can JavaScript be in the head?

JavaScript in body or head: Scripts can be placed inside the body or the head section of an HTML page or inside both head and body.

How do you add codes to your head?

Hover over your content and click Edit. In the content editor, click the Settings tab, then click Advanced options. In the Additional code snippets section, enter your code snippet into the Head HTML or Footer HTML field.

What goes in the head section of HTML?

The <head> HTML element contains machine-readable information (metadata) about the document, like its title, scripts, and style sheets.


2 Answers

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.

like image 28
Alex Ivasyuv Avatar answered Sep 29 '22 03:09

Alex Ivasyuv


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.

like image 168
noah Avatar answered Sep 29 '22 02:09

noah