Giter VIP home page Giter VIP logo

Comments (4)

getify avatar getify commented on July 27, 2024

dynamic script loading completely de-couples script loading (normally done with script tags) from the window's dom-ready and onload events. That's the "Blocking" in LABjs name in fact, that it removes (by default) such blocking behavior.

There's actually no way for any dynamic script loader of this nature to guarantee dom-ready/onload blocking when scripts are loading. So, essentially, those two events will proceed independent of whether a script loads quickly (from cache) or slowly (over the net).

The problem you're describing was, in a similar fashion, present in jQuery prior to version 1.4, in that jQuery was not, when loaded, checking if dom-ready had already passed when it was loaded. As such, there was a race condition, that if jQuery got there before dom-ready, all the $(document).ready(...) waiting blocks worked. But if jQuery came in after dom-ready, and it didn't check for that, those waiting blocks would all just sit there forever hoping dom-ready would happen.

Starting with jQuery 1.4+, the code base checks for a passed dom-ready by looking for document.readyState, which (for the most part) resolves this problem. In conjunction, because FF before 3.6 didn't have this property, LABjs added a little page-level hack to make that property present, so that the jQuery check would work correctly.

A similar thing seems to be happening with your checking for "onload". The logic (I'm guessing) inside Event.observe(...) is simply waiting to hear if onload happens, and if it does, it fires. But I'm betting that onload is firing before this listener gets a chance to register, thus meaning it just waits forever in vain.

There's no "window.loaded" property to rely on like there was for dom-ready with "readyState", so the situation is a little tougher/more manual to correct for.

Basically, my suggestion would be something like this:

var isWindowLoaded = false;
Event.observe(window,'load',function(){ isWindowLoaded = true; });

$LAB.script("...blah.js").wait(function(){
   if (isWindowLoaded) {
      doSomethingNow();
   }
   else {
      Event.observe(window,'load',doSomethingNow);
   }
});

The key is to have an event listener that executes immediately during page-load (not in waiting for something to happen inside LABjs) that looks for when the "onload" happens... if it happens before $LAB has a chance to run, it sets a boolean property to true, so that when the $LAB.wait(...) call runs, it will see that the event has already passed and it'll just proceed immediately. Otherwise, it knows it needs to still wait for the event, so it registers a handler and waits for it to occur.

I hope this is helpful. Let me know if you have other issues.

from labjs.

haarts avatar haarts commented on July 27, 2024

That was ... amazing. You have no idea how much you helped me out. Thank you for your clear explaination and solution.

from labjs.

getify avatar getify commented on July 27, 2024

so glad that helped. hope your issues are resolved now! :)

from labjs.

gbakernet avatar gbakernet commented on July 27, 2024

This helped me too. Although, I had to include a cross browser solution for window onload in the initial JS loader footprint.

from labjs.

Related Issues (20)

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.