Last updated: Mar 7, 2024
Reading timeยท4 min
Use the document.querySelector()
method to get the information from a meta
tag.
Once you select the meta
tag, you can access its attributes using dot
notation or the getAttribute()
method.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="title" content="Example meta title" /> <meta name="description" content="Example meta description" /> <meta property="og:title" content="The Rock" /> <meta property="video" content="https://youtu.be/HZ8uXq5VG2wk" /> </head> <body> <h2>bobbyhadz.com</h2> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const metaTitle = document.querySelector('meta[name="title"]'); // ๐๏ธ <meta name="title" content="Example meta title"> console.log(metaTitle); // ๐๏ธ Example meta title console.log(metaTitle.content); // ๐๏ธ title console.log(metaTitle.name); // ๐๏ธ Example meta title console.log(metaTitle.getAttribute('content'));
We used the document.querySelector()
method to select the meta
element that has a name
attribute of title
.
const metaTitle = document.querySelector('meta[name="title"]');
Notice that we alternate between single and double quotes so that the string selector is not closed prematurely.
The query selected the following element.
<meta name="title" content="Example meta title" />
We can access the attributes of the meta
element using dot notation or the
getAttribute()
method.
// ๐๏ธ <meta name="title" content="Example meta title"> console.log(metaTitle); // ๐๏ธ Example meta title console.log(metaTitle.content); // ๐๏ธ title console.log(metaTitle.name); // ๐๏ธ Example meta title console.log(metaTitle.getAttribute('content'));
The same approach can be used to access the meta
tag that has its name
attribute set to description
.
const metaDescription = document.querySelector( 'meta[name="description"]', ); // ๐๏ธ <meta name="description" content="Example meta description"> console.log(metaDescription); // ๐๏ธ Example meta description console.log(metaDescription.content); // ๐๏ธ description console.log(metaDescription.name); // ๐๏ธ Example meta description console.log(metaDescription.getAttribute('content'));
You can also create a reusable function that takes the name of the attribute and
its value and returns the corresponding meta
element.
const metaDescription = getMetaElement('name', 'description'); // ๐๏ธ <meta name="description" content="Example meta description"> console.log(metaDescription); // ๐๏ธ Example meta description console.log(metaDescription.content); // ๐๏ธ description console.log(metaDescription.name); // ๐๏ธ Example meta description console.log(metaDescription.getAttribute('content')); function getMetaElement(attribute, value) { return document.querySelector(`meta[${attribute}="${value}"]`); }
The getMetaElement
function takes the name and value of the attribute and
returns the matching meta
element.
You can also use this approach to get data from meta OG (Open Graph) tags.
Suppose we have the following HTML.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta property="og:title" content="The Rock" /> <meta name="title" content="Example meta title" /> <meta name="description" content="Example meta description" /> <meta property="video" content="https://youtu.be/HZ8uXq5VG2wk" /> </head> <body> <h2>bobbyhadz.com</h2> <script src="index.js"></script> </body> </html>
We want to select the following OG meta
tag.
<meta name="title" content="Example meta title" />
Here is the related JavaScript.
const metaOgTitle = getMetaElement('property', 'og:title'); // ๐๏ธ <meta property="og:title" content="The Rock" /> console.log(metaOgTitle); // ๐๏ธ The Rock console.log(metaOgTitle.content); // ๐๏ธ The Rock console.log(metaOgTitle.getAttribute('content')); function getMetaElement(attribute, value) { return document.querySelector(`meta[${attribute}="${value}"]`); }
Notice that the value of the attribute (og:title
) is placed in quotes.
function getMetaElement(attribute, value) { return document.querySelector(`meta[${attribute}="${value}"]`); }
Here is the equivalent example without using a reusable helper function.
const metaOgTitle = document.querySelector( 'meta[property="og:title"]', ); // ๐๏ธ <meta property="og:title" content="The Rock" /> console.log(metaOgTitle); // ๐๏ธ The Rock console.log(metaOgTitle.content); // ๐๏ธ The Rock console.log(metaOgTitle.getAttribute('content'));
meta
tags in the documentIf you need to access data from all meta
tags in the document:
document.getElementsByTagName()
method to select the meta tags.for...of
loop to iterate over the collection of meta
tags.name
and content
attributes of each meta tag.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="title" content="Example meta title" /> <meta name="description" content="Example meta description" /> <meta property="video" content="https://youtu.be/HZ8uXq5VG2wk" /> <meta property="og:title" content="The Rock" /> </head> <body> <h2>bobbyhadz.com</h2> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const metaTags = document.getElementsByTagName('meta'); for (const metaTag of metaTags) { console.log(metaTag.name, metaTag.content); }
We used a for...of loop to iterate over the collection of meta tags.
The for...of
statement is used to loop over iterable objects like arrays,
strings, Map
, Set
and NodeList
objects and generators
.
On each iteration, we access the name
and content
attributes of the meta
tag.
meta
element.You can use an if
statement if you need to filter for specific meta elements
based on their attributes.
const metaTags = document.getElementsByTagName('meta'); for (const metaTag of metaTags) { if (metaTag.name === 'description') { console.log(metaTag.name, metaTag.content); } }
The only meta
element that meets the condition is the one that has its name
attribute set to description
.
You can also use the getAttribute()
method to access the attributes of the
meta element.
const metaTags = document.getElementsByTagName('meta'); for (const metaTag of metaTags) { if (metaTag.getAttribute('name') === 'description') { // ๐๏ธ description, Example meta description console.log( metaTag.getAttribute('name'), metaTag.getAttribute('content'), ); } }
The only parameter the getAttribute()
method takes is the name of the
attribute whose value to retrieve.
You can also access the meta
tags using their index in the collection.
Suppose, we have the following head
tag.
<head> <meta charset="UTF-8" /> <meta name="title" content="Example meta title" /> <meta name="description" content="Example meta description" /> <meta property="video" content="https://youtu.be/HZ8uXq5VG2wk" /> <meta property="og:title" content="The Rock" /> </head>
Here is an example of accessing the meta tags by their index in the collection.
const metaTags = document.getElementsByTagName('meta'); // ๐๏ธ Example meta title console.log(metaTags[1].content); // ๐๏ธ Example meta description console.log(metaTags[2].content);
Here is an equivalent example that uses the getAttribute()
method.
const metaTags = document.getElementsByTagName('meta'); // ๐๏ธ Example meta title console.log(metaTags[1].getAttribute('content')); // ๐๏ธ Example meta description console.log(metaTags[2].getAttribute('content'));
JavaScript indexes are zero-based, so the first element in a collection has an
index of 0
.
We didn't use index 0
to skip the following meta
tag.
<meta charset="UTF-8" />
You can learn more about the related topics by checking out the following tutorials: