Discovering the Shadow DOM
Tags: iframe, javascript, webdev, shadowdom
Published: 2019-07-08 17:37:59

---
title: Discovering the Shadow DOM
published: true
tags: iframe,javascript,web-development,shadow-dom
canonical_url: https://blog.logrocket.com/discovering-the-shadow-dom-e541d74aefb3
---

![](https://cdn-images-1.medium.com/max/958/1*ct_7DotEJPOAOavhtBtqpA.jpeg)

We are all fully aware of the [Document Object Model](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction) (DOM). Thanks to it, we can manipulate the structure and the content of an HTML page via JavaScript in an effective way.

Of course, the DOM is not free from defects. One of them, for example, is the lack of encapsulation, that is the possibility to protect a part of the document structure from global processing, such as CSS rules application and JavaScript manipulation.

In this article, we will analyze an example of this issue, how it is traditionally tackled and how we can resolve it by using the[Shadow DOM](https://www.w3.org/TR/shadow-dom/), a standard feature that has been around for a while but is not as well-known by front-end developers.

[![](https://cdn-images-1.medium.com/max/1024/1*GPjaPKNNUYHU8EsA3Z0JGA.png)](https://logrocket.com/signup/)

### Using the DOM

Consider the following JavaScript code:

```jsx
const myGlossary = {};
​
myGlossary.renderSearchBox = function (idContainer) {
 const box = document.createElement("div");
 const form = document.createElement("form");
 const label = document.createElement("label");
 const textbox = document.createElement("input");
 const button = document.createElement("input");
 const definition = document.createElement("div");
​
 label.htmlFor = "txtTerm";
 label.innerHTML = "<h4>My Glossary</h4>";
 form.appendChild(label);
​
 textbox.type = "text";
 textbox.id = "txtTerm";
 form.appendChild(textbox);
​
 button.type = "submit";
 button.value = " Search ";
 form.appendChild(button);
 form.appendChild(document.createElement("br"));
​
 box.style.textAlign = "center";
 box.style.fontFamily = "Verdana";
 box.appendChild(form);
​
 definition.style.padding = "15px";
 box.appendChild(definition);
​
 document.getElementById(idContainer).appendChild(box);
​
 form.onsubmit = () => {
   const term = textbox.value.trim().toLocaleLowerCase();
   const definitions = {"html": "HTML is the Web markup language.", "css": "CSS is the language describing the style of HTML documents."};
​
   if (term) {
     definition.innerHTML = definitions[term] || `'${term}' not found.`;
  }
   return false;
};
}
```

Here, we define an object with a single method, _renderSearchBox()_. This method takes the identifier of an HTML element as its argument and appends to it the elements needed to build up a search textbox like the one shown below:

![](https://cdn-images-1.medium.com/max/457/0*z51BvlckXakaHUP8)

This search textbox is intended to allow the user to find glossary definitions from an online service. For simplicity, we have embedded the term definitions in the code.

When the user inserts a term in the textbox and clicks the search button, the _form.onsubmit()_ event listener is executed. This function simply shows the definition associated with the inserted term, if any, otherwise it shows an appropriate message.

The following picture shows what the user will see when they submit the _HTML_ term into the search text box:

![](https://cdn-images-1.medium.com/max/496/0*tt3ZTMmUffZH8YFO)

You can use the myGlossary object in an HTML page:

```jsx
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
 <title>Discovering the Shadow DOM</title>
</head>
<body>
 <div id="divBox"></div>
 <script type="text/javascript" src="myGlossaryBox.js"></script>
 <script type="text/javascript">
 myGlossary.renderSearchBox("divBox");
 </script>
</body>
</html>
```

As you can see, we imported the script from _myGlossary.js_ file and invoked the _renderSearchBox()_ method by passing the _divBox_ string, as the identifier of the only _div_ element in the page.

You can try this code on [CodePen](https://codepen.io/andychiare/pen/gQpgRN).

### The problem with the DOM

The search box created by the script shown above works as expected. It is also self-contained enough to be re-used in other projects.

But what happens if you use the search box inside a page with a markup?

```jsx
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
 <title>Discovering the Shadow DOM</title>
 <style>
   h4 {
     text-align: center;
     color: red;
  }
 </style>
</head>
<body>
   <div>
       <h4>Welcome to my great page!</h4>
   </div>
   <!-- other markup -->
 <div id="divBox"></div>
   <!-- other markup -->
 <script type="text/javascript" src="myGlossaryBox.js"></script>
 <script type="text/javascript">
 myGlossary.renderSearchBox("divBox");
 </script>
</body>
</html>
```

In this page, you notice a CSS rule re-defining the colour of the _h4_ element. Of course, the page designer meant to customize the welcome message. However, this rule also affects the title of the imported search box:

![](https://cdn-images-1.medium.com/max/427/0*jBl7ZOGbjRWG1FEY)

Since the DOM is a global entity, the CSS rule shown above affects any _h4_ element in the page, even the ones dynamically added from an external script. You have no way of protecting the specific _h4_ element defined when building the search box. You cannot encapsulate it if you just rely on the DOM.

But this issue is not only related to the CSS rule application. Imagine what happens if somewhere on the page you have the following JavaScript statement:

```jsx
document.getElementById("txtTerm").style.visibility = "hidden";
```

Your search textbox magically disappears:

![](https://cdn-images-1.medium.com/max/397/0*URUIaci4BwoQPIHK)

Try this code on [CodePen](https://codepen.io/andychiare/pen/YRXNEB).

You cannot prevent the _txtTerm_ id from being used elsewhere on the page. And you cannot control what any external code may do, intentionally or not, with the DOM elements you created for your search box.

Again, the DOM is a global entity: any element you add to it is publicly accessible which means conflicts may happen.

### Using iframes

The most common solution to get around this problem is to embed the portion of DOM representing your component in a separate DOM, i.e. in a separate HTML page. This page is then appended to the main page via an iframe. This is the approach used by Google to allow developers to embed its maps and its videos from YouTube and Twitter.

Let’s take a look at how to implement this idea. Rearrange the previous code like this:

```jsx
const myGlossary = {};
​
myGlossary.renderSearchBox = function (idContainer) {
 const iframe = document.createElement("iframe");
 const box = document.createElement("div");
 const form = document.createElement("form");
 const label = document.createElement("label");
 const textbox = document.createElement("input");
 const button = document.createElement("input");
 const definition = document.createElement("div");
​
 label.htmlFor = "txtTerm";
 label.innerHTML = "<h4>My Glossary</h4>";
 form.appendChild(label);
​
 textbox.type = "text";
 textbox.id = "txtTerm";
 form.appendChild(textbox);
​
 button.type = "submit";
 button.value = " Search ";
 form.appendChild(button);
 form.appendChild(document.createElement("br"));
​
 box.style.textAlign = "center";
 box.style.fontFamily = "Verdana";
 box.appendChild(form);
​
 definition.style.padding = "15px";
 box.appendChild(definition);
​
 iframe.style.border = 0;
 iframe.style.height = "200px";
 document.getElementById(idContainer).appendChild(iframe);
​
 iframe.contentDocument.body.appendChild(box);
​
 form.onsubmit = () => {
   const term = textbox.value.trim().toLocaleLowerCase();
   const definitions = {"html": "HTML is the Web markup language.", "css": "CSS is the language describing the style of HTML documents."};
​
   if (term) {
     definition.innerHTML = definitions[term] || `'${term}' not found.`;
  }
   return false;
};
}
```
The first difference, with respect to the previous version, is the creation of an iframe element:

```jsx
const iframe = document.createElement("iframe");
```

The second difference is the way you append the portion of DOM representing the search box to the external element. With this approach, you don’t directly append the box element to the external element. You append the iframe element to the external element and then append the box element to the body of the document associated with the iframe. The code would look something like this:

```jsx
iframe.style.border = 0;
 iframe.style.height = "200px";
 document.getElementById(idContainer).appendChild(iframe);
​
 iframe.contentDocument.body.appendChild(box);
```

With this approach, you will no longer get the conflicts you experienced with the previous version of the search box.

You can try this on[ CodePen](https://codepen.io/andychiare/pen/pQJRae).

### The problem with iframes

In the iframe-based approach, you should have noticed a couple of assignments concerning the style of the iframe. Let’s recall these two assignments in the following:

```jsx
 iframe.style.border = 0;
 iframe.style.height = "200px";
```

These assignments define the border thickness and the height of the iframe itself. They are fundamental to provide the illusion that the search box is integrated with the main page.

But while the first instruction fulfills its task well, the second may be insufficient. In fact, you may have a chance that the size of the iframe doesn’t fit the size of the document, so you could have an effect similar to the following:

![](https://cdn-images-1.medium.com/max/415/0*0E4mbRkKhgSWDRIK)

This is an ugly effect and you should avoid it.

Iframes are not very responsive. In addition, iframes are designed to embed another full document in the current page. This leads to more complexity when we want to pass data from the parent document to the child document, for example, in order to configure some elements in the child document.

### Entering the Shadow DOM

A better solution to manage the encapsulation of a portion of the DOM inside the DOM of an HTML page is to use the _Shadow DOM_. This is[a set of standard APIs](https://dom.spec.whatwg.org/#shadow-trees) enabling the encapsulation at the DOM level by allowing you to create a sort of private DOM for an HTML element.

Let’s see how to use the _Shadow DOM_ to avoid the issues of both the global DOM and the iframes. Rewrite the JavaScript code implementing the search box as follows:

```jsx
const myGlossary = {};
​
myGlossary.renderSearchBox = function (idContainer) {
 const box = document.createElement("div");
 const form = document.createElement("form");
 const label = document.createElement("label");
 const textbox = document.createElement("input");
 const button = document.createElement("input");
 const definition = document.createElement("div");
​
 label.htmlFor = "txtTerm";
 label.innerHTML = "<h4>My Glossary</h4>";
 form.appendChild(label);
​
 textbox.type = "text";
 textbox.id = "txtTerm";
 form.appendChild(textbox);
​
 button.type = "submit";
 button.value = " Search ";
 form.appendChild(button);
 form.appendChild(document.createElement("br"));
​
 box.style.textAlign = "center";
 box.style.fontFamily = "Verdana";
 box.appendChild(form);
​
 definition.style.padding = "15px";
 box.appendChild(definition);
​
 const container = document.getElementById(idContainer)
 container.attachShadow({mode: "open"});
​
 container.shadowRoot.appendChild(box);
​
 form.onsubmit = () => {
   const term = textbox.value.trim().toLocaleLowerCase();
   const definitions = {"html": "HTML is the Web markup language.", "css": "CSS is the language describing the style of HTML documents."};
​
   if (term) {
     definition.innerHTML = definitions[term] || `'${term}' not found.`;
  }
   return false;
};
}
```

In this case, the only difference with respect to the original version of the script consists of the following statements:

```jsx
const container = document.getElementById(idContainer);
container.attachShadow({mode: "open"});
​
container.shadowRoot.appendChild(box);
```

Instead of directly attaching the box element to the external element, we first create a _Shadow DOM_ to the external element via attachShadow(). This method is[available for almost any HTML element](https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow) and it generates the root node for the _Shadow DOM_ of the element.

This root node is then accessible through the shadowRoot property. In the example above, we appended to the shadowRoot of the external element the box element representing the search box. These two simple instructions grant us protection against conflicts and undesired effects when the search box is included in an arbitrary HTML page. You can try this code on[ CodePen](https://codepen.io/andychiare/pen/mQJRLN).

You may notice that we passed a literal object as an argument to the attachShadow() method. This object allows us to specify the creation mode of the _Shadow DOM_. In our example, we set the value _open_ as the _Shadow DOM_ mode. This means that the _Shadow DOM_ will be hidden to standard global DOM manipulation such as CSS rules applications, node selection and the like.

However, the _Shadow DOM_ is exposed to the outside world, so its elements will be accessible via JavaScript. In other words, you can access the _Shadow DOM_ of the search box from any page with something like this:

```jsx
const divBox = document.getElementById("divBox");
divBox.shadowRoot.querySelector("h4").innerText = "Changed from outside"
```

This code replaces the text of the label associated with the search textbox.

If you don’t want this to happen, you may choose to set the _closed_ value:

```jsx
container.attachShadow({mode: "closed"});
```

In this case, the _Shadow DOM_ will not be accessible at all.

### Conclusion

Iframes can still be useful when they do the work they were born for: embedding an independent external page in the current page. However, we have now seen how the _Shadow DOM_ can replace the typical encapsulation workaround based on iframes which can have a lot of benefits.
* * *
### Plug: [LogRocket](https://logrocket.com/signup/), a DVR for web apps

[![](https://cdn-images-1.medium.com/max/1024/1*s_rMyo6NbrAsP-XtvBaXFg.png)](https://logrocket.com/signup/)<figcaption><a href="https://logrocket.com/signup/">https://logrocket.com/signup/</a></figcaption>

[LogRocket](https://logrocket.com/signup/) is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single page apps.

[Try it for free.](https://logrocket.com/signup/)
* * *
The post [Discovering the Shadow DOM](https://blog.logrocket.com/discovering-the-shadow-dom-e541d74aefb3/) appeared first on [LogRocket Blog](https://blog.logrocket.com).