Skip to main content

JavaScript function to get the meta tag content value. Normally located in the HTML head tag.

/**
 * Get Meta Tag Content
 *
 * @param {string} metaName The meta tag name.
 * @return {string} The meta tag content value, or empty string if not found.
 */
function getMetaContent(metaName) {
    var metas = document.getElementsByTagName('meta');
    var re = new RegExp('\\b' + metaName + '\\b', 'i');
    var i = 0;
    var mLength = metas.length;

    for (i; i < mLength; i++) {
        if (re.test(metas[i].getAttribute('name'))) {
            return metas[i].getAttribute('content');
        }
    }

    return '';
}