how to get <head>
content of the current page
Asked
Active
Viewed 7.4k times
44
3 Answers
59
You could use the javascript DOM API like this:
var headContent = document.getElementsByTagName('head')[0].innerHTML;

user113716
- 318,772
- 63
- 451
- 440
-
6Just use `document.head.innerHTML`. [Can I use?](http://caniuse.com/#search=document.head) – Zaz Oct 14 '16 at 01:14
-
1@AlexandruEftimie while `document.head` is probably preferred in most modern browsers. It won't work in Internet Explorer 8 or earlier – Michael Johnston Sep 09 '18 at 02:16
13
You can use an element selector to get <head>
, for example:
$("head")
//for example:
alert($("head").html()); //alerts the <head> children

Nick Craver
- 623,446
- 136
- 1,297
- 1,155
6
Simply as:
document.head
which returns an object DOM Object.
document.head.innerHTML
which returns text, like: '<title>Title of the page...'.

Moritz Ringler
- 9,772
- 9
- 21
- 34

egoproxy
- 303
- 3
- 12
-
1Check browser compatibility for `document.head`. I believe it's not supported in IE till v9. – xr280xr Aug 18 '16 at 20:25
-
Yes. It's supported from IE9 [developer.mozilla.org/en-US/docs/Web/API/Document/head](https://developer.mozilla.org/en-US/docs/Web/API/Document/head) – egoproxy Aug 24 '16 at 08:32