Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1607 Can JavaScript reference the meta tag name and values in an HTML document, i.e. author, description, keywords?

You are here: irt.org | FAQ | JavaScript | Misc | Q1607 [ previous next ]

Only in Internet Explorer 4+ with:

<head>
<title>Meta Data</title>
<meta id="author" name="Author" content="Martin Webb">
</head>

<body>
<script language="JavaScript"><!--
if (document.all)
  document.write('Author: ' + document.all.Author.content);
//--></script>
</body>

</html>

The following works with any browser that supports the W3C DOM Level 1 (at the time of writing both Internet Explorer 5+ and Netscape Navigator 6+):

<html>

<head>
<title>Meta Data</title>
<meta name="Author" content="Martin Webb">
<meta name="Author" content="A.N. Other">
<meta name="Description" content="A sample html file for extracting meta data">
<meta name="Keywords" content="JavaScript, DOM, W3C">

</head>

<body>
<script language="JavaScript"><!--
if (document.getElementsByName) {
  var metaArray = document.getElementsByName('Author');
  for (var i=0; i<metaArray.length; i++) {
    document.write(metaArray[i].content + '<br>');
  }

  var metaArray = document.getElementsByName('Description');
  for (var i=0; i<metaArray.length; i++) {
    document.write(metaArray[i].content + '<br>');
  }

  var metaArray = document.getElementsByName('Keywords');
  for (var i=0; i<metaArray.length; i++) {
    document.write(metaArray[i].content + '<br>');
  }
}
//--></script>
</body>

</html>

©2018 Martin Webb