Skip to content

Instantly share code, notes, and snippets.

@kiranbabu189
Created August 29, 2020 13:31
Show Gist options
  • Save kiranbabu189/e2808348105c6f73b36f797867812f1a to your computer and use it in GitHub Desktop.
Save kiranbabu189/e2808348105c6f73b36f797867812f1a to your computer and use it in GitHub Desktop.
Meta tag content generator
function createMetaTags(tags, type) {
var comment = document.createComment(type);
document.getElementsByTagName('head')[0].appendChild(comment);
tags.forEach(element => {
var attribute = Object.keys(element)[0];
var link = document.createElement('meta');
link.setAttribute(attribute, element[attribute]);
var metaKey = element[attribute].split(':');
var content = metaKey.length > 1 ? metaValues[metaKey[1]] : metaValues[metaKey[0]]
link.content = content;
document.getElementsByTagName('head')[0].appendChild(link);
});
};
var logoUrl = 'https://test/logo.svg';
var author = 'test';
var metaValues = {
title: "test",
description: "test",
url: "https://www.test.com/",
image: logoUrl,
'image:src': logoUrl,
type: "Website",
creator: author,
author: author,
site: author
};
var ogMeta = [
{ property: 'og:title' },
{ property: 'og:type' },
{ property: 'og:url' },
{ property: 'og:image' },
{ property: 'og:description' }
];
var twitterMeta = [
{ name: 'twitter:title' },
{ name: 'twitter:type' },
{ name: 'twitter:url' },
{ name: 'twitter:image' },
{ name: 'twitter:description' },
{ name: 'twitter:creator' },
{ name: 'twitter:image:src' }
];
var schemaMeta = [
{ itemprop: 'title' },
{ itemprop: 'type' },
{ itemprop: 'url' },
{ itemprop: 'image' },
{ itemprop: 'description' }
];
var meta = [
{ name: 'title' },
{ name: 'author' },
{ name: 'description' }
];
createMetaTags(meta, 'Primary Meta Tags');
createMetaTags(ogMeta, 'Open Graph / Facebook');
createMetaTags(twitterMeta, 'Twitter');
createMetaTags(schemaMeta, 'Schema.org markup for Google+');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment