views:

1129

answers:

4

I need to extract the document's "description" meta tag.

The default way would be to use document.getElementsByTagName('META') and then iterate through the array - as found in: http://www.rgagnon.com/jsdetails/js-0070.html

But I'm wondering if there's no other quicker, "one line of code" approach. I'm not familiar with xPath - but maybe that could work? Any ideas?

+2  A: 

sure...

var desc = document.getElementsByName('description')[0].getAttribute('content');

This presumes that there is a Meta Tag named description of course.

To be more complete, this would catch the description regardless of case.

function getDesc(){
  var metas = document.getElementsByTagName('meta');
  for(var i=0;mLen=metas.length;i<mLen;i++){
    if(metas[i].getAttribute('name').toLowerCase() == 'description'){
      return metas[i].getAttribute('content');
    }
  }
  return null;//or empty string if you prefer
}
var desc = getDesc();
scunliffe
Thanks for the suggestion, but (a) this will return other named entities, not only Meta tags, and (b) I didn't mention it originally, but I attempt to find a case-insensitive situation, as some sites use name "Description", some use "DESCRIPTION", and so forth...
CamelHive
@CamelHive - the [0] syntax should get you the very first element with a name attribute... 99.999% of the time this will be the meta tag. For case sensitivity... yeah, this would fail. I'll add a not-quite 1 line version.
scunliffe
A: 

You can do it with XPath (in clients supporting document.evaluate) but that would probably be an overkill:

document.evaluate('//*[@name="description"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
kangax
That's sweet, I should really get my head around XPath - thanks. The problem with case sensitivity can be resolved (so I learned) by extending the XPath considerably. In any case, thanks!
CamelHive
To replace that `namedItem`, you can try accessing element right off of a `NodeList`. IIRC, most modern browsers (?) still support such element resolution (from the DOM0 days) - `document.getElementsByTagName('meta')['description']`. I wouldn't recommend using this shorthand though. Safer to just enumerate over elements checking for `name` property or use some kind of abstraction (such as selector utilities provided by most of JS libraries or your own, perhaps simplified, implementation)
kangax
A: 

Hi,

Scunliffe, I think your suggestion will definitely do the trick, at least the multi-line function will.

I came up with a solution that doesn't run on Webkit (Chrome + Safari) but which is very compact:

var metas = document.getElementsByTagName('META');
var value = (metas.namedItem ("description") || metas.namedItem ("Description") || metas.namedItem ("DESCRIPTION") || {}).content;

This will utilize the namedItem() function over the NodeList object, looking for most likely ways another programmer might write "description". Note that this will retrieve the values of tag's attribute, while ignoring case for attribute names:

<meta name="description" content="my description" />
<meta NAME="Description" CoNtEnT="my description" />

But alas (Oy Vey) the nameItem() doesn't work on Safari & Chrome :-( Maybe there's an alternative approach for those browsers...

CamelHive
A: 

It also depends on what you mean by 'quickest'- do you mean the shortest code you can write, or the fastest response from the browser?

For the quickest response, just look at the meta elements in the head element, and pick out any with the name 'description'.

{
  var s=[], metas=document.getElementsByTagName('head')[0].getElementsByTagName('meta');
  for(var i=0,L=metas.length;i<L;i++){
    if(metas[i].name=='description')s[s.length]=metas[i].content;
  }
  s= s.join(',');
}
kennebec