44

how to get <head> content of the current page

vbence
  • 20,084
  • 9
  • 69
  • 118
faressoft
  • 19,053
  • 44
  • 104
  • 146

3 Answers3

59

You could use the javascript DOM API like this:

var headContent = document.getElementsByTagName('head')[0].innerHTML;
user113716
  • 318,772
  • 63
  • 451
  • 440
  • 6
    Just 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

You can give it a try here

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
  • 1
    Check 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