I am learning Web Components. To grab a template, I needed to do this:
<template>
<div>The sky is blue</div>
</template>
<script>
var tmpl = (document.currentScript||document._currentScript).ownerDocument.querySelector('template');
Instead of:
var tmpl = document.querySelector('template');
I don't understand this part at all: (document.currentScript||document._currentScript).ownerDocument
What is currentScript, what is ownerDocument? What is the purpose? Why does it work. Note that the template code I have shown above gets pulled in via the link element.
Note, this is related to this post and this article. I AM just trying to understand the keywords, I don't have any particular problem in mind.
Definition taken from here: https://html.spec.whatwg.org/multipage/dom.html#dom-document-currentscript
And I believe that
_currentScriptcomes from a Polyfill.The
ownerDocumentfor anything in the DOM Tree will bedocumentbut for a<template>or a file loaded through an HTML Import theownerDocumentwill be a different document. This is whydocument.querySelector()won't work to get the<template>of a file loaded using an HTML Import.