Skip to content

Instantly share code, notes, and snippets.

@Zatvobor
Created March 3, 2015 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zatvobor/547eae496555f0c0f17e to your computer and use it in GitHub Desktop.
Save Zatvobor/547eae496555f0c0f17e to your computer and use it in GitHub Desktop.
ShadowDom: Shadow hole
<!doctype html>
<html class="no-js" lang="en">
<head>
<style>
h1, h2, h3 {5
color: red;
}
</style>
<template>
<h2>h2</h2>
<div id="h3-container"></div>
<script>
var div = document.querySelector('#h3-container');
var h3 = document.createElement('h3');
h3.textContent = 'h3';
div.appendChild(h3);
</script>
</template>
</head>
<body>
<h1>h1</h1>
<div id="host">
</div>
<script>
var template = document.querySelector('template');
var clone = document.importNode(template.content, true);
var host = document.querySelector('#host');
host.createShadowRoot().appendChild(clone);
</script>
</body>
</html>
@Zatvobor
Copy link
Author

Zatvobor commented Mar 3, 2015

Here is a browser shot, http://take.ms/qX15X

@Zatvobor
Copy link
Author

Zatvobor commented Mar 3, 2015

Here is a work around with result, http://take.ms/4VUaq

<!doctype html>
<html class="no-js" lang="en">
<head>
  <style>
    h1, h2, h3 {
      color: red;
    }
  </style>
  <template>
    <h2>h2</h2>
  </template>
</head>
<body>
  <h1>h1</h1>
  <div id="host"></div>
  <script>
    var template  = document.querySelector('template');
    var clone     = document.importNode(template.content, true);
    var host      = document.querySelector('#host');
    var shadow    = host.createShadowRoot();
    shadow.appendChild(clone);

    var h3  = document.createElement('h3');
    h3.textContent = 'h3';
    shadow.appendChild(h3);
  </script>
</body>
</html>

@Zatvobor
Copy link
Author

Zatvobor commented Mar 3, 2015

In conclusion, you definitely can't query elements from template's script except put DOM entites inside shadow hole.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment