Using WebView tag and grid.html to dynamically display data

Electron’s webview tag is based on Chromium’s webview, which is simply a custom element using shadow DOM to wrap an iframe element inside it. Unlike an iframe, the webview runs in a separate process than your app. Most methods called on the webview from the host page require a synchronous call to the main process.

For more information visit:


We can tell webview to open some web page, in our case, we want to render an existing web page grid.html stored in our project dir. In order to tell our webview what to show, we’ll need to define its source:

<webview src="./grid.html" autosize></webview>

When autosize attribute is enabled, the webview container size cannot be less than the minimum values (CSS min-height and min-width) or greater than the maximum (CSS max-heightand max-width). A webview tag can be styled just like a regular html tag, using the style or class attributes.

There are also a number of event listener for the webview tag. The did-start-loading is called when the web page begins to load, did-finish-load is called when the web page is loaded, did-fail-load is called when the web page fails to load and dom-ready is called when the web page completely loaded and parsed.

Inserting webview tag to index.html

We’ll create a webview tag and load the grid.html file by providing the src path:

<webview src="./grid.html" style="flex:1 1 auto;"></webview>
 onload = () => {
  const webview = document.querySelector('webview');
  webview.addEventListener('dom-ready', () => {
  • The onload event executes when index.html fully loaded and parsed.
  • The dom-read event executes when the webview completely loaded and parsed the grid.html file.
  • We then includes the renderer.js file

Creating grid.html file

In grid.html file, we will create an HTML table with three columns to display duplicate files. The first column will show a check box to select a file, the second column will show the size of the file and the third column will show the file name including the full path:

   <th id="col1">
    <input type="checkbox" id="checkBoxSelectAll">
   <th id="col2">Size</th>
   <th id="col3">File</th>

The renderer.js file will send the duplicate files, their sizes and their hashes to grid.html in an array form. The grid.html will append the received data to the table dynamically.

In previous tutorials, we already learned how to insert table rows and cell dynamically:

  1. Insert table rows and cells dynamically
  2. Creating resizable table grid. (Follow this tutorial if you want a resizable table grid for grid.html).

Let’s create the complete grid.html file step by step. First we insert the style tag after opening the head tag to style HTML table:

table th{text-align:left}
tbody{background-color:#eee;cursor:pointer;border-bottom: 15px solid white}
td,th{padding:5px 15px;border:1px solid #fff}
td:nth-child(3) {
 overflow-wrap: break-word;
 word-wrap: break-word;
 word-break: break-all;
 word-break: break-word;

Next, we insert the table and table heading tags:

   <th id="col1"><input type="checkbox" id="checkBoxSelectAll"></th>
   <th id="col2">Size</th>
   <th id="col3">File</th>

Next, we write JavaScript code which we created on to dynamically insert rows and cells:

var table = document.getElementsByTagName('table')[0],
tbodys = [];
function addRows(data) {
 if (!data) return;
 var tbody = tbodys[];
 var size = data.size;

 if (tbody === undefined){
  tbody = table.createTBody(); =;
  tbodys[] = tbody;

 data.files.forEach(file => {
  var tr = tbody.insertRow(),
  td1 = tr.insertCell(),
  td2 = tr.insertCell(),
  td3 = tr.insertCell(),
  cb = document.createElement('input');
  cb.type = 'checkbox';
  cb.value = file;

  var sz = document.createTextNode(size);

  var ph = document.createTextNode(file);
 var tableHeight = table.offsetHeight;
 return ((document.body.scrollHeight+50)+'px');
Optionally, you can also add resizable grid code from

In next tutorial, we’ll modify the renderer.js file to communicate with grid.html file. To download grid.html and index.html files, follow these links: grid.html and index.html.