nickromano / deferred-scripts

Defer <script> tag loading until needed.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

deferred-scripts

Build Status Coverage Status npm

Defer <script> tag loading until needed.

If you have third party scripts you are unable to load until the user has given consent you can use this to defer them.

Installation

npm install deferred-scripts

Example

var deferredScripts = new DeferredScripts();

// addScript(sourceURL, onLoadCallback)
deferredScripts.addScript('https://www.google-analytics.com/ga.js', function() {
	// Onload
	console.log('Loaded GA');
});

// addPartial(sourceURL) - requires jquery available
deferredScripts.addPartial('./my-partial.html');

// onLoadCallback is optional
deferredScripts.addScript('https://www.google-analytics.com/ga.js');

// When you're ready to load the scripts
deferredScripts.loadScripts();

About

Defer <script> tag loading until needed.

License:MIT License


Languages

Language:JavaScript 79.0%Language:HTML 21.0%