Created
July 18, 2013 21:49
-
-
Save ceee/6033429 to your computer and use it in GitHub Desktop.
fetch meta attribute in vanilla js (with og support)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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