I made a simple example using Web Components with two custom elements (v1) where one is nested in another. index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="import" href="app-container.html">
</head>
<body>
<app-container></app-container>
</body>
</html>
app-container.html:
<link rel="import" href="toolbar.html">
<template id="app-container">
<app-toolbar></app-toolbar>
</template>
<script>
customElements.define('app-container', class extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({ mode: 'open' });
const content = document.currentScript.ownerDocument.querySelector('#app-container').content;
shadowRoot.appendChild(content.cloneNode(true));
}
});
</script>
toolbar.html:
<template id="app-toolbar">
<p>Ok!</p>
</template>
<script>
customElements.define('app-toolbar', class extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({ mode: 'open' });
const content = document.currentScript.ownerDocument.querySelector('#app-toolbar').content;
shadowRoot.appendChild(content.cloneNode(true));
}
});
</script>
But in the toolbar.html document.currentScript
is the same as in the app-container.html and hence querySelector('#app-toolbar')
can't find template with id app-toolbar
. How to solve this problem?
Example tested on Chrome 55 (without polyfill).
document.currentScript
contains a reference to the script that is currently parsed and executed. Therefore it is not valid anymore for your purpose when theconstructor()
function is called (from another script).Instead you shoud save its value in a variable at the beginning of the script, and use this variable in the constructor:
If you have multiple scripts, you should use distinct names.
Alternately, you can encapsulate the ephemeral value in a closure:
Here the value
document.currentScript.ownerDocument
is assigned to theowner
argument which is still defined correctly whenconstructor()
is called.owner
is locally defined so you can use the same name in the other document.