开发者

Writing a custom javascript event

I'm writing some code that will get executed before the DOM loads, basically, using Modernizr to get scripts. Now my issue is that I want to show a loading animation if the DOM loads and the scripts are still loading.

Modernizr is executed in the head. If I put the code to use document.getElementById in the head also, error is thrown because the DOM hasn't loaded. Now I have no idea how to solve this.

Here is the code I have so far:

<head>
 <script>
     var FileManager = {
         IsLoading = false;
         LoadRequiredFiles: function (config) {
             config = config || {};
             this.OnLoading = config.onLoadingCallback;
             this.OnComplete = config.onCompleteCallback;
             this.IsLoading = true;
             if (this.OnLoading) {
                 this.OnLoading();
             }

             var self = this;
             Modernizr.load([{
                 load: '/jquery.min.js',
                 complete: function () {
                         if (self.OnComplete) {
                   开发者_运维百科          self.OnComplete();
                         }
                         self.IsLoading = true;

                 }
             },
            ]);
         }
     };
     var globalLoadingId = 'globalLoader';
     FileManager.LoadRequiredFiles({
         onLoadingCallback: function () {
             document.getElementById(globalLoadingId).style.display = 'block';
         },
         onCompleteCallback: function () {
             document.getElementById(globalLoadingId).style.display = 'none';
         }
     });
</script>

I used to execute this code below the <body> tag, and it worked. Now I moved it into the <head>. So I used to pass 2 callbacks to it. Now I'd rather attach events to it and handle them in the body (assuming thats where the DOM is loaded).

What I'd like to do:

<head>
<script>
    FileManager.LoadRequiredFiles();
</script>
</head>

<body>
<script>

    //Bind the event, not sure if this is even possible in javascript.
    FileManager.OnCompleted += fileManagerCompleted;

    fileManagerCompleted()
    {
         document.getElementById(globalLoadingId).style.display = 'none';
    }

    if(FileManager.IsLoading)
    {
        document.getElementById(globalLoadingId).style.display = 'block';
    }
</script>
</body>


The page is your canvas for display. You can't show anything before it loads. It sounds more like you want a very small page to load (quickly) where you could display your progress and then your code could dynamically load/display the rest of the page with ajax calls and javascript showing progress as it goes. That's the only way to get out in front of the rest of the page load that I know of.


The only entirely reliable way to run a script that manipulates the DOM is to use the body onload event. (window.onload is popular, but not quite 100% reliable.)

There are some browsers that implement a onDocumentReady event that can be kind-of-sort-of faked in IE, but I don't recommend its use.


Using getElementById will not, by itself, throw an error if used in the head. You might be causing an error because you aren't checking the returned value, which will be null if an element with the specified id wasn't found, e.g.

var el = document.getElementById('foo');

if (el) {
  // do somethig with el
} else {
  // el wasn't found
}

Your problem is how to display the image only if the scripts are still loading and the page is visible. The simple answer is don't use client-side script loading, do it at the server. :-)

If you want to persist with script loading, add a class to the loading image, say "hideOnLoad". Have a callback from the last script load that sets the rule to "display: none" (just create and add style sheet with that one rule using script).

Now you just include the loading image as the first element in the body with a class of "hideOnLoad", knowing that when scripts have finished loading they will hide the image regardless of whether it (or any other element with the same class) existed at the time or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜