2

So, I know how to use JS or jQuery, etc., to display a "Loading" message while content is loading. I have created a fairly large webapp with a number of JS dependencies and I want to display a "loading" message while all the scripts are loading.

My head tag has a number of <script src=…> tags in it and I want to display a loading message instantly when the user visits the page, and then remove it when all the scripts are loaded.

What's the best way to do this?

MikeC8
  • 3,513
  • 2
  • 22
  • 32

2 Answers2

0

Then use $ajax function of jquery to download this javascript files and the add script element in head tag after downloading completes.

like this:

// display loading message here 

 $ajax("javascriptfile.js",function(file){

  // attach downloaded file to head tag now

 });
Tushar Ahirrao
  • 10,773
  • 16
  • 61
  • 95
0

You probably need to lazy loading of the script. The last example from this Lazy Loading show to load .js via YUI. The code from that example is included below for your reference:

var HelloWorld = {
    is_loaded: false,
    lazyLoad: function(callback) {
        var loader = new YAHOO.util.YUILoader();
        loader.addModule({
            name: "helloworld",
            type: "js",
            fullpath: "yui_ex/helloworld.js"
        });
        loader.require("helloworld");
        if (callback) {
            loader.onSuccess = callback;
        }
        loader.insert();
    },
    sayIt: function() {
        var args = arguments;
        HelloWorld.lazyLoad(function() { HelloWorld.sayIt.apply(HelloWorld, args); });
    }
};

Note that you could possibly load the loading image initially and remove it in the callback function. Reading SO Question JQuery to load Javascript file dynamically, you could also use $.getScript() to do the same thing.

You could also find another example in this link

Community
  • 1
  • 1
momo
  • 20,477
  • 8
  • 36
  • 38