Comments (4)
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.
That was ... amazing. You have no idea how much you helped me out. Thank you for your clear explaination and solution.
from labjs.
so glad that helped. hope your issues are resolved now! :)
from labjs.
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)
- BOM issues HOT 4
- HTTP2 or SPDY and LABjs HOT 2
- swallowing errors doesn't seem right HOT 1
- It's nearing time for 3.0 HOT 31
- LAB inserts scripts at the beginning of the <head> HOT 7
- how to use LAB when Chrome blocks inline scripts ? HOT 2
- LABjs regular expression breaks for Windows apps
- Why not add localStorage to cache the scripts? HOT 6
- What is the role of first line?
- labjs.com is down HOT 1
- Recovering from 404 HOT 1
- Refactor out arguments.callee HOT 1
- huge longshot, but... might be a bug with LAB.js here. HOT 8
- www.labjs.com is not working
- LABjs blocking the executation of jquery each()
- setAttribute("as", "script") in chrome 51.0.2704.84 window7 messy code
- Seeking for a stable v3.0 package
- Current project status HOT 2
- [Question] Does LABjs scrub out the debugger keyword? HOT 2
- JsDelivr - I can't see '.js' files HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from labjs.