document.ready and ExecuteOrDelayScriptLoaded
Emily Wong
I have a dilemma - my javascript code needs to be executed when the DOM is ready. However, at the same time I need to be able to hook up to the load event of another script. So hypothetically speaking I need something like this:
ExecuteOrDelayUntilScriptLoaded(getData, "sp.js"); function getData() { (document.ready(function() { //my code to get data from sharepoint list. })); }Only the latter does not seem to work.
Please suggest!
3 Answers
Why not to do it like this?
$(document).ready(function() { ExecuteOrDelayUntilScriptLoaded(function getData(){ //your code to get data from sharepoint list. }, "sp.js"); }); 1 Try
$(document).ready(function() { $.getScript('sp.js', function() { //your code to get data from sharepoint list. });
});For even more control over script loading, try a script loader like the simple and lightweight yepnope.js or the more complex LABjs.
2If you are using jQuery, MooTools, or any other library - there is a standard function you can hook into, which checks if the DOM and your assets are loaded.
For example, in jQuery: