Coder Social home page Coder Social logo

include-module's Introduction

Include-Module

Replicates CSS's "@import" functionality for front-end JS

Similar to ES6's experimental "import" feature

@src - string, source of the script to 'include' @callback - function, callback to be executed after the script is loaded

@options - JSON object

  • @before - string, source of script to 'include' before
  • @after - string, source of script to 'include' after
  • @replace - string, script source or regex of source to replace with our new script
  • @async - boolean, whether or not our new script should be async

Options should only include ONE of the three destination selectors (before, after, replace), otherwise the first argument is the one that is used.

Extend implementation found on https://jsfiddle.net/1vrkw1pc/

**NOTE: This is not a true implementation of the experimental 'import' function in ES6, we are not actually creating a reference to any objects that may be included in the script you are importing, and we do not choose which variables or objects to import. We are simply adding an external script to our page dynamically.

Basic Usage:

if (deviceSize == 'mobile') include('mobile.js');
else include('web.js');

Callbacks:

There is an optional callback that we can pass into our function to execute after our script has been successfully loaded. This is a good place to put any code that may be dependant on the script you are importing.

// We can pass in an existing function like this
include('script.js', callbackFunction());

// Or we can use an anonymous callback as our function
include('script.js', function(){
    console.log('loaded');
    // Execute dependant code here
});

Options:

We can pass additional options into our function via a JSON object, such as whether the script we are importing should load asyncronously or not, whether we want to insert our script before or after a particular script that is in the header (this is handy for scripts that are dependant on other scripts), or whether we want to completely replace a script with our new one (for example, if we want to replace one version of jQuery with another).

// Add our script with an asynchronous attribute (async is false by default)
include('script.js', {async: true});

// Add our script before a specified script
// If before, after, or remove are not specified, the script is appended to the bottom of the <head>
include('script.js', {after: 'scriptToAddAfter.js'});
include('script.js', {before: 'scriptToAddBefore.js'});
include('script.js', {replace: 'scriptToReplace.js'});

// Putting everything all together
include('script.js', 
function(){
    console.log('Script has been loaded before scriptToAddBefore.js successfully!');
},
{
    async: false, 
    before: 'scriptToAddBefore.js'
});

include-module's People

Contributors

devonwieczorek avatar

Watchers

James Cloos avatar  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.