3

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.

Supersharp
  • 29,002
  • 9
  • 92
  • 134
VSO
  • 11,546
  • 25
  • 99
  • 187
  • https://www.reddit.com/r/learnjavascript/comments/10i4xmn/comment/j5d5mlr/?utm_source=share&utm_medium=web2x&context=3 – aderchox Jan 22 '23 at 07:19

2 Answers2

3

_currentScript was used in the old webcomponentsjs polyfill (version 0.x). Now if you use polyfill version 1.x it is always undefined.

You should then use only document.currentScript:

<template>
  <div>The sky is blue</div>
</template>

<script>
    var tmpl = document.currentScript.ownerDocument.querySelector('template');
</script>

document.currentScript.ownerDocument will give you a reference to the imported document (with <link rel='import'>), where the current <script> is running and where the <template> is defined.

See this SO answer (and this one) for further details on this topic.

Supersharp
  • 29,002
  • 9
  • 92
  • 134
2

Definition taken from here: https://html.spec.whatwg.org/multipage/dom.html#dom-document-currentscript

document.currentScript

Returns the script element, or the SVG script element, that is currently executing, as long as the element represents a classic script. In the case of reentrant script execution, returns the one that most recently started executing amongst those that have not yet finished executing.

Returns null if the Document is not currently executing a script or SVG script element (e.g., because the running script is an event handler, or a timeout), or if the currently executing script or SVG script element represents a module script.

And I believe that _currentScript comes from a Polyfill.

The ownerDocument for anything in the DOM Tree will be document but for a <template> or a file loaded through an HTML Import the ownerDocument will be a different document. This is why document.querySelector() won't work to get the <template> of a file loaded using an HTML Import.

Community
  • 1
  • 1
Intervalia
  • 10,248
  • 2
  • 30
  • 60
  • I am able to use `_currentScript` without any polyfills in Chrome. I am guessing my code is not even hitting that due to the null coalescing. Regardless, I am still having a hard time following the formal definition. – VSO Nov 21 '17 at 18:35
  • 1
    All I know is that I always check both. Maybe I should understand the `why` but once I found how to get it to work, I stopped looking. ;). – Intervalia Nov 21 '17 at 18:38
  • Hey, I appreciate the help, you did get my code up and running =) – VSO Nov 21 '17 at 18:40