Skip to content

Instantly share code, notes, and snippets.

@ceee
Created July 18, 2013 21:49
Show Gist options
  • Save ceee/6033429 to your computer and use it in GitHub Desktop.
Save ceee/6033429 to your computer and use it in GitHub Desktop.
fetch meta attribute in vanilla js (with og support)
/**
* Fetches a meta (+ open graph) attribute from HTML
*
* @returns value The request content of the meta tag (nullable)
*/
var getMetaAttribute = function( attribute )
{
var key = null;
var value = null;
var meta = document.getElementsByTagName('meta');
// search desired tag
for( var i = 0, l = meta.length; i < l; i += 1 )
{
key = meta[i].name || meta[i].getAttribute('property');
if( !key )
{
continue;
}
// found an open graph tag
if( key.split(':')[0] === 'og' )
{
key = key.split(':')[1];
}
// is this the tag we want?
if( key === attribute )
{
value = meta[i].content;
}
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment