0

This question is not a duplicate of Get meta data attribute in javascript. I'd like to get meta name by language.

I have the following in HTML code:

<meta name="DC.Subject" xml:lang="pl" content="wróg" /> <meta name="DC.Subject" xml:lang="en" content="enemy" /> 

There are also other meta name tags in HTML.

It is quite easy to get the content of the first DC.Subjectby using:

document.getElementsByName("DC.Description")[0].getAttribute("content"))

and the second by

document.getElementsByName("DC.Description")[1].getAttribute("content"))

But this is not language specific. However, how do I get the content of DC.Description by using xml:lang= in JavaScript?

5
  • Take a look at this question, I think it's the same as the one you're asking - stackoverflow.com/questions/949341/… Commented Oct 30, 2016 at 18:25
  • 1
    Possible duplicate of Get meta data attribute in javascript Commented Oct 30, 2016 at 18:29
  • @LGSon Your post does not explain how to get attribute by language. Commented Oct 30, 2016 at 18:56
  • I see that now .. was too quick ... but this one does: stackoverflow.com/questions/23034283/… Commented Oct 30, 2016 at 18:57
  • @RoseRobertson I do not want to obtain the declared language of the document. Commented Oct 30, 2016 at 18:58

2 Answers 2

1

You may use document.querySelector as follows:

var plMeta = document.querySelector('[xml\\:lang="pl"][name="DC.Subject"]'); console.log(plMeta);
<meta name="DC.Subject" xml:lang="pl" content="wróg"> <meta name="DC.Subject" xml:lang="en" content="enemy">

Here's a related Q&A to get more in touch with CSS selectors involving namespaces: Is it possible to use HTML's .querySelector() to select by xlink attribute in an SVG?

Sign up to request clarification or add additional context in comments.

13 Comments

It's a dupe so please close it as such
@LGSon I believe OP's issue isn't getting a <meta> but getting it by xml:lang which involves escaping :
Error: { "message": "SyntaxError: missing ; before statement", "filename": "http://stacksnippets.net/js", "lineno": 14, "colno": 64 }
@connexo I hope you didn't downvote my answer because of an extra parenthesis :D
@MatíasFidemraizer You are right. I do not want just to get <meta> (my post shows I can do it). I'd like to know how to get it by xml:lang However, in the webpage there are more than one meta with xml:lang. How do I get <meta name="DC.Subject" (there are other <meta name) by language?
|
0

You need to escape the problematic : character in the attribute name:

var xml_lang_elements = document.querySelectorAll('[xml\\:lang][name="DC.Subject"]'); for (var i = 0; i < xml_lang_elements.length; i++) { console.log(xml_lang_elements[i].getAttribute('content')); }
<meta name="DC.Subject" xml:lang="pl" content="wróg" /> <meta name="DC.Subject" xml:lang="en" content="enemy" />

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.