0

I'm trying to split my long JavaScript code into different libraries.

I'm trying to write a wrapper script that will load all my libraries:

//this file loads all the scripts to the page    
$(document).ready(function(){
    var fileName = getCurrentFileName();
    loadScript("scripts/pico/popups.js");
    loadScript("scripts/pico/effects.js");
    loadScript("scripts/pico/pico.js");
    loadScript("scripts/pico/facebook.js");
    if (fileName != null)
        loadScript("script/pico/" + fileName + ".js");
});    
/**
     * Load a script to the body element
     * @param name the name of script file.js
     */
function loadScript(name){
    // Adding the script tag to the body
    var body = document.getElementsByTagName('body')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = name;
    // Fire the loading
    body.appendChild(script);
}    
/**
     * Get the current HTML file name
     * @returns {*} the name of the file or null
     */
function getCurrentFileName(){
    try {
        var fileName;
        fileName = document.location.pathname.match(/[^\/]+$/)[0];
        return fileName.split('.')[0];
    }
    catch (e){
        return null;
    }
}

but i think facebook.js is loaded before pico.js has finished loading - and these are functions from pico.js inside facebook.js..

how can i make sure the libraries will be loaded in the order i enter it ?

3
  • You would need to use the onreadystatechange handler to ensure that they are loaded in the order required. Commented Jan 22, 2014 at 21:01
  • stackoverflow.com/a/21246366/1959948 Commented Jan 22, 2014 at 21:03
  • code.google.com/p/minify Combines, minifies, and caches JavaScript and CSS files on demand to speed up page loads. Commented Jan 22, 2014 at 21:05

3 Answers 3

1

You would need to use the onreadystatechange handler to ensure that they are loaded in the order required.

//this file loads all the scripts to the page

$(document).ready(function () {
    var fileName = getCurrentFileName();
    loadScript("scripts/pico/popups.js");
    loadScript("scripts/pico/effects.js");
    loadScript("scripts/pico/pico.js",function(){
        loadScript("scripts/pico/facebook.js");
        if (fileName != null) loadScript("script/pico/" + fileName + ".js");
    });
});

/**
 * Load a script to the body element
 * @param name the name of script file.js
 */
function loadScript(name,callback) {
    // Adding the script tag to the body
    var body = document.getElementsByTagName('body')[0];
    var script = document.createElement('script');
    script.onreadystatechange = function(){
        if (script.readyState === "complete" && $.isFunction(callback)) {
            callback();
        }
    }
    script.type = 'text/javascript';
    script.src = name;
    // Fire the loading
    body.appendChild(script);
}

Since jQuery is already included, it's even easier.

//this file loads all the scripts to the page

var fileName = getCurrentFileName();
$.getScript("scripts/pico/popups.js");
$.getScript("scripts/pico/effects.js");
$.getScript("scripts/pico/pico.js",function(){
    $.getScript("scripts/pico/facebook.js");
    if (fileName != null) $.getScript("script/pico/" + fileName + ".js");
});
Sign up to request clarification or add additional context in comments.

Comments

1

Have you consider using requirejs

http://www.joezimjs.com/javascript/lazy-loading-javascript-with-requirejs/

Here is sample version:

  1. You can download Require here.

  2. The sample is based on this folder structure :

    public

    • index.html
    • scripts

      • app.js
      • lib

        ** jquery-1.10.2.js

        ** require.js

3 . From Code:

html

<!DOCTYPE html><html>
<head><title>Sample Test</title>
<script src="scripts/lib/require.js"></script>  <!-- downloaded from link provide above-->
<script src="scripts/app.js"></script></head>
<body><h1>My Sample Project</h1><div id="someDiv"></div></body></html>

application configuration app.js

requirejs.config({
    baseUrl: 'scripts',

    paths: {
        app: 'app',
        jquery: 'lib/jquery-1.10.2' //your libraries/modules definitions
    }
});

// Start the main app logic. loading jquery module
require(['jquery'], function ($) {
        $(document).on('ready',function(){
            $('#someDiv').html('Hello World');
        });
 });

1 Comment

There is of course some options available using JQUERY api.jquery.com/jQuery.getScript
0

What you are looking for is called "Dependency Management". There are several solutions out there for this problem.

One big one is RequireJS: http://requirejs.org/docs/start.html

See also:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.