Created
May 27, 2011 14:54
-
-
Save psema4/995408 to your computer and use it in GitHub Desktop.
Altering Metatags With JS
This file contains hidden or 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
<!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