homepage Welcome to WebmasterWorld Guest from 54.198.224.121
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Script to get value of a meta tag?
I want to get one value, say the keywords. Could be used as bookmarklet
R1chard




msg:3224336
 12:02 am on Jan 19, 2007 (gmt 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.

 

Little_G




msg:3224345
 12:18 am on Jan 19, 2007 (gmt 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]

bedlam




msg:3224346
 12:26 am on Jan 19, 2007 (gmt 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

Little_G




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

Hi,

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

Andrew

bedlam




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

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

-b

Little_G




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

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

Andrew

R1chard




msg:3228152
 11:01 pm on Jan 22, 2007 (gmt 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)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved