Welcome to WebmasterWorld Guest from 54.226.133.245

Forum Moderators: open

Message Too Old, No Replies

Script to get value of a meta tag?

I want to get one value, say the keywords. Could be used as bookmarklet

     
12:02 am on Jan 19, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 19, 2003
posts:165
votes: 0


So, I'm after some code that will give me the value (ie "content") of a document's meta tag. I am able to get the following, which will alert the first such tag on a page:

javascript: alert(document.getElementsByTagName("meta")[0].content)

But what I'm after is a particular one, not the first in an arbitrary order. Suppose I wanted the text corresponding to the keywords, is there a simple way of modifying the array index above to get that? Or maybe an even simpler one? indexOf( doesn't seem to work. I guess I'm overlooking something...

Any help would be much appreciated. Thanks in advance.

12:18 am on Jan 19, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 11, 2006
posts:481
votes: 0


Hi,

try:

function getMetaContents(mn){
var m = document.getElementsByTagName('meta');
for(var i in m){
if(m[i][i].name == mn){
return m[i].content;
}
}
}

[/i]Andrew

[edited by: Little_G at 12:19 am (utc) on Jan. 19, 2007]

12:26 am on Jan 19, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 30, 2003
posts:728
votes: 0


Hi,

try:

function getMetaContents(mn){ ...

Not quite what was asked for. If you need the value of a specific meta element's 'content' attribute, the only way to do it is for you to give your script some kind of parameters to look for. You probably need something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>metaKeywords() test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="foo" content="bar" />
<meta name="keywords" content="bar, foo, fubar" />
<script type="text/javascript">
function metaKeywords() {
metaCollection = document.getElementsByTagName('meta');

for (i=0;i<metaCollection.length;i++) {
nameAttribute = metaCollection[i].name.search(/keywords/);

if (nameAttribute!= -1) {
alert(metaCollection[i].content);
}
}
}
</script>
</head>

<body onload="metaKeywords();">
<p>Test</p>
</body>
</html>

Sorry for the total lack of indents :-

-b

12:37 am on Jan 19, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 11, 2006
posts:481
votes: 0


Hi,

Ok, sorry I didn't put in usage, thought it was self explanatory: to get keywords call

getMetaContents('keywords');

Andrew

12:47 am on Jan 19, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 30, 2003
posts:728
votes: 0


It's only self-explanatory to non-idiots though, which is presumably why I didn't get it... :-)

-b

12:55 am on Jan 19, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 11, 2006
posts:481
votes: 0


Aww, I don't think your an idiot, just special. ;)

Andrew

11:01 pm on Jan 22, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 19, 2003
posts:165
votes: 0


Yeah, in the end I used a variation on the above function. What I was originally after was making a Firefox extension to display a website in google maps via the location tags, but then I decided it was simple enough to use a bookmarklet, and this would also mean it could be used in Opera etc. (and in fact, I also discovered an existing extension later which did it anyway, so it's a good job I didn't bother doing it that way!)

If anybody is interested, the code is as follows:

javascript: t=document.getElementsByTagName(%22meta%22); s=%22%22; for(i=0;i<t.length;i++){if(t[i].name==%22ICBM%22 ¦¦ t[i].name==%22geo.position%22) s=t[i].content;}; if(s==%22%22) alert("No data available"); else window.open(%22http://maps.google.co.uk/?q=%22+s)

It makes for a good bookmar that can then be called whenever you want. I guess it could be adapted to use your other favorite mapping engine (Yahoo, Mapquest, etc)