Giter VIP home page Giter VIP logo

laterscript's Introduction

<laterscript>

Defer & programmatically trigger loading of <script> tags.

This is useful for external, third party JS dependencies that you want to embed in a base HTML template but don't want to load & parse until a later, programmatically-invoked time.

Usage

Include the laterscript JS tag above your first <laterscript> tag.

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/msmathers/laterscript@master/js/laterscript.js"></script>

Use a <laterscript> tag for inline or remote JS scripts. Make sure it has an ID.

<laterscript id="myscript" type="text/javascript" src="/path/to/file.js">
</laterscript>

Activate the script via laterscript.load(). Returns a promise that resolves once the script has been loaded & parsed.

laterscript.load('myscript').then(function(scriptEl) {
  console.log('script loaded!')
})

The <laterscript> element is replaced with a <script> element, which gets parsed and loaded once it hits the DOM.

<script id="myscript" type="text/javascript" src="/path/to/file.js">
</script>

Example

<html>
  <head>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/msmathers/laterscript@master/js/laterscript.js"></script>
    <laterscript id="jqueryjs" type="text/javascript" src="https://code.jquery.com/jquery-3.5.0.min.js"></laterscript>
    <laterscript id="usejquery" type="text/javascript">
      $(document).ready(function() {
        $('body').html('<h1>jQuery loaded!</h1>')
      })
    </laterscript>
  </head>
  <body>
    <button id="load-query-btn">Load jQuery</button>
  </body>
  <script type="text/javascript">
    (function() {
      var btn = document.getElementById('load-query-btn');
      btn.onclick = function() {
        laterscript.load('jqueryjs')
          .then(function() { laterscript.load('usejquery') })
      }
    })()
  </script>
</html>

laterscript's People

Contributors

msmathers avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.