Closed ghost closed 12 years ago
In fact, to know when a file is loaded we can use two functions :onreadystatechange
, used by IE and Opera, and onload
, used by IE>8 and all others.
And it begins!
Some browsers/versions hadn't parse code yet when the callback is called (based on onreadystatechange
and onload
states). Many libraries provide their own system to handle that behavior. With toast, I've chosen to let the developer handle it with a powerful way (but a bit redundant, I guess).
That system isn't always needed. It depends on what you want to load. And, even if we often just need to do a if(!window.lib) return false
, sometimes that condition could be more complicated (like nest toast calls, verify multiple library load state, etc...).
Ok, I understand.
I've used loaders before, for example, I've used head.js
and I had some issue, that I couldn't fix:
Loader loads jquery, plugins, and other files (client-code). It works fine when I just open the site. But when I reload it (by pressing F5) - problems may occur - callback is executed early than $ is defined.
So, is it precisely this problem?
I think, I've try toast with if (!window.lib) return false
protection some time later.
It could be the same problem, yes. Linked to the browser cache, per example. I don't know :)
From your wiki, I've read:
Sometimes, on some browsers, scripts are not parsed yet when we want to use them (like calling a function from that script). To resolve that issue, toast provides a simple way:
When it could happen, I don't really understand. If loader provide callback function, it means, that callback will be called when loader has already load the script, than functions from this script are available.
So, I need
if (!some_var_from_script){ return false;}
protection in every script I load/