An open API service indexing awesome lists of open source software.

https://github.com/msmathers/laterscript

Defer & programmatically trigger loading of <script> tags.
https://github.com/msmathers/laterscript

defer-loading load-js script-tag

Last synced: 3 months ago
JSON representation

Defer & programmatically trigger loading of <script> tags.

Awesome Lists containing this project

README

        

# <laterscript>

Defer & programmatically trigger loading of `` 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.
```html
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/msmathers/laterscript@master/js/laterscript.js">
```

Use a `` tag for inline or remote JS scripts. Make sure it has an ID.
```html

```

Activate the script via `laterscript.load()`. Returns a promise that resolves once the script has been loaded & parsed.
```js
laterscript.load('myscript').then(function(scriptEl) {
console.log('script loaded!')
})
```

The `` element is replaced with a `` element, which gets parsed and loaded once it hits the DOM.

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

```

## Example

```html





$(document).ready(function() {
$('body').html('

jQuery loaded!

')
})



Load jQuery


(function() {
var btn = document.getElementById('load-query-btn');
btn.onclick = function() {
laterscript.load('jqueryjs')
.then(function() { laterscript.load('usejquery') })
}
})()

```