-1

I have only a boilerplate html document with three meta tags, when I try to do: console.log(document.meta) I get undefined why? When trying all other elements I have the node displayed. I don't want to access it for modification I want to know why document.head and document.body return the head and body element respectively.

  • Possible duplicate of [How do I get the information from a meta tag with JavaScript?](https://stackoverflow.com/questions/7524585/how-do-i-get-the-information-from-a-meta-tag-with-javascript) – Mat Mar 06 '19 at 17:28

2 Answers2

0

document.meta isn't a valid property on the document object. Perhaps try:

document.getElementsByTagName('meta')
Blue
  • 22,608
  • 7
  • 62
  • 92
  • What is it then? and what make a tag a valid document property? any reference on the theory behind that. – sammy_khaleel Mar 06 '19 at 15:13
  • @sammy_khaleel I recommend checking out: https://developer.mozilla.org/en-US/docs/Web/API/Document. `document` is an object. If you want to access tags, you need to use the functions that are available to access that. Simply typing `document.meta` doesn't actually do anything. – Blue Mar 06 '19 at 15:15
  • why console.log(document.body) and console.log(document.head) will display the respective elements in chrome console? – sammy_khaleel Mar 06 '19 at 15:29
  • @sammy_khaleel Because there is exactly one body, and exactly one head tag to every HTML document. For shorthand, they've added a convenience method to refer to these elements. Since meta tags, span tags, div tags, and all other tags are optional, it would be silly to add them all the document object. – Blue Mar 06 '19 at 15:34
  • Refer to the [document Properties](https://developer.mozilla.org/en-US/docs/Web/API/Document#Properties) docs, for more info. – Blue Mar 06 '19 at 15:34
  • can you add the last two replays to the answer so that I can accept it as my answer. – sammy_khaleel Mar 06 '19 at 16:33
  • @sammy_khaleel You can feel free to edit my answer if you wish. The comments here are a part of the answer, so I don't feel it's entirely necessary. – Blue Mar 07 '19 at 11:48
0

you can try it :

console.log(document.getElementsByTagName('meta'));
EmilienSE
  • 21
  • 3