最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - editing the <head> section with javascript - Stack Overflow

programmeradmin3浏览0评论

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.

Share Improve this question edited Mar 3, 2010 at 18:43 Gumbo 655k112 gold badges791 silver badges851 bronze badges asked Mar 3, 2010 at 18:40 beninobenino 5972 gold badges7 silver badges18 bronze badges 3
  • 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
Add a ment  | 

5 Answers 5

Reset to default 3

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.

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.

发布评论

评论列表(0)

  1. 暂无评论