Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

How to get the data from meta tags in JavaScript

3 Answers

0 votes
<meta name="title" content="title - text" />
<meta name="description" content="description - text" />
const title = document.querySelector('[name=title]');
const description = document.querySelector('[name=description]');

console.log(title.content); 
console.log(description.content); 


  
    
    
/*
run:
    
"title - text"
"description - text"
    
*/

 



answered Jun 15, 2021 by avibootz
0 votes
<meta name="title" content="title - text" />
<meta name="description" content="description - text" />
function getMetaData(metaName) {
  const metanames = document.getElementsByTagName('meta');

  for (let i = 0; i < metanames.length; i++) {
    if (metanames[i].getAttribute('name') === metaName) {
      return metanames[i].getAttribute('content');
    }
  }
  return '';
}

console.log(getMetaData('title'));
console.log(getMetaData('description'));


  
    
    
/*
run:
    
"title - text"
"description - text"
    
*/

 



answered Jun 15, 2021 by avibootz
0 votes
function getMetaData(attr,val) {
  	return document.querySelector(`[${attr}=${val}]`).content;
}


console.log(getMetaData('name','title')); 
console.log(getMetaData('name','description')); 



  
    
    
/*
run:
    
"title - text"
"description - text"
    
*/

 



answered Jun 15, 2021 by avibootz

Related questions

3 answers 259 views
259 views asked Apr 19, 2014 by avibootz
1 answer 160 views
160 views asked Jun 28, 2016 by avibootz
1 answer 232 views
1 answer 93 views
...