Created
July 19, 2018 01:40
-
-
Save chellimiller/138eb43a0a5b3cdedb1fa0245fea04b6 to your computer and use it in GitHub Desktop.
Playing around with the HTML5 Template tag
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
<!doctype html> | |
<html> | |
<title>PAGE</title> | |
<meta charset="utf-8"> | |
<body onload="inform()"> | |
<template id="chelli-link"> | |
<style> | |
a { | |
color: red; | |
text-decoration: none; | |
font-size: 4em; | |
} | |
a:hover { | |
color: black; | |
} | |
</style> | |
<a></a> | |
</template> | |
<script> | |
var proto = Object.create(HTMLElement.prototype, { | |
createdCallback: { | |
value: function() { | |
var template = document.querySelector('#chelli-link'); | |
console.log(template); | |
var cloneTemplate = document.importNode(template.content, true); | |
var links = cloneTemplate.querySelectorAll("a"); | |
links[0].textContent = 'helloTesting'; | |
links[0].setAttribute('href', 'https://www.w3schools.com/jsref/met_element_setattribute.asp'); | |
this.createShadowRoot().appendChild(cloneTemplate); | |
} | |
} | |
}); | |
document.registerElement('chelli-link', {prototype: proto}); | |
</script> | |
<chelli-link></chelli-link> | |
</body> | |
<script> | |
function inform() { | |
console.log('BODY RELOAD'); | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
document.registerElement(); is deprecated. Use customElements.define(name, constructor, options);
https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define