Skip to content

Instantly share code, notes, and snippets.

@psema4
Created May 27, 2011 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psema4/995408 to your computer and use it in GitHub Desktop.
Save psema4/995408 to your computer and use it in GitHub Desktop.
Altering Metatags With JS
<!doctype html>
<html lang="en">
<head>
<title>Altering Metatags At Runtime</title>
<meta id="meta-test" name="Copyright" content="(C)2011 Some User" />
</head>
<body>
<h1>Altering Metatags At Runtime</h1>
<p id="output">(Click "Check Metatag")</p>
<button onclick="checkMeta('meta-test')">Check Metatag</button>
<button onclick="changeMeta('meta-test', '(C)2011 Some Other User')">Change Metatag Contents</button>
<script type="text/javascript">
window.updateCounter = 1;
function checkMeta(id) {
var el = document.getElementById(id);
try {
document.getElementById('output').innerHTML = el.getAttribute('content');
} catch(e) {
console.log(e);
}
}
function changeMeta(id, newValue) {
var el = document.getElementById(id);
var updateText = newValue + ' [Update #' + window.updateCounter++ + ']';
try {
el.setAttribute('content', updateText);
checkMeta(id);
} catch(e) {
console.log(e);
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment