Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

tomickigrzegorz/importlazy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

An example of using dynamic file loading.

Dynamic import() js file with aync/await

Live DEMO

If you want this example to work in IE10, just add it to index.html

<script type="text/javascript">
  if (!('Promise' in window)) {
    var script = document.createElement("script");
    script.src = "https://polyfill.io/v3/polyfill.min.js?features=Promise";
    document.getElementsByTagName('head')[0].appendChild(script);
  }
</script>

How its work?

If you want to load a js file, all you have to do is add data-module="js-module-name". An example below:

<button id="background" class="button-primary" data-module="background">change the background</button>

In this case, the background.js file will be dynamically loaded after clicking the button.