Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

SP.SOD is a nice feature of SharePoint, but how should I use it? For example, I use these lines of code

ExecuteOrDelayUntilScriptLoaded(function () {
   //code
}, "custom.js");

If I understand correctly this code will be invoked when custom.js is loaded. There are several functions that can load this script. For example

SP.SOD.executeFunc('custom.js', null, callbackfunction)

What the best practice to use SP.SOD.executeFunc and ExecuteOrDelayUntilScriptLoaded together?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

This code is the basic implementation of LoadSodByKey, this is the best implementation of the function as it has the override to allow you to have a callback for when the script is loaded.

After jquery is loaded, the anonymous function will write "Hello World" to the console

<script type="text/javascript">
   LoadSodByKey("js/jquery-1.9.0.min.js", function () {
       console.log("Hello world!")
   });
</script>

These are my thus far best practices for loading and using scripts, and I will continue to update this post as time goes on, and understanding of this increases.

[UPDATE]

A full working compatible pattern for doing this is: Note the called JS file needs to be registered, and run the Notify SOD command

// Callback method
var callback = function () {
    // Code goes here
};

// Expected namespace after loaded
var namespace = "SP.RequestExecutor";

// SOD Key of target js file
var key = "sp.requestexecutor.js";

// Load straight away, or in queue
var bSync = false;

SP.SOD.executeFunc(key, namespace, function () {
    console.log(key + ": Type available (" + namespace + ")");
}, bSync);

// Execute when script has notified loading (Event call)
SP.SOD.executeOrDelayUntilScriptLoaded(callback, key);
share|improve this answer
2  
you can find this post more helpful. –  Alexander Feb 4 '13 at 10:07
    
That is a good blog post, and probably the first on the subject, I have to highlight there will be several more coming shortly. I have found several bugs with FireFox and MDS and I am sharing them with several other JS SP nuts, workarounds will be published for anything found. Stay tuned to my twitter for updates. –  Hugh Wood Feb 4 '13 at 11:11
    
can you give an example of callback.. in case of executeOrDelayUntilScriptLoaded please. –  variable 2 days ago
    
There you go dude –  Hugh Wood 2 days ago

SP.SOD.executeFunc works with OnDemand scripts while ExecuteOrDelayUntilScriptLoaded does not.

Here is an excellent resource on SP.SOD:

http://www.ilovesharepoint.com/2010/08/sharepoint-scripts-on-demand-spsod.html

ExecuteOrDelayUntilScriptLoaded(func, scriptName) schedules an asynchronous callback function (func) which will be called when the script has signaled finished loading. Signaled finished loading means that the script has called notifyScriptLoadedAndExecuteWaitingJobs(scriptName). All SharePoint built-in scripts will call notifyScriptLoadedAndExecuteWaitingJobs when they have finished loading. ExcuteOrDelayUntilScriptLoaded does not trigger loading an on demand script (SOD)!

SP.SOD.executeFunc(key, functionName, fn) is used to load on demand scripts (ScriptLink.OnDemand=true). The “key” parameter must match to the ScriptLink’s Name property (Use small letters for key, because an issue with string normalizing in RegisterSodDep)

share|improve this answer
    
Vardharman, can you copy the relevant code snippets to your answer please incase that site is taken down in the future and the link is broken. The link does contain the best practice - verified. –  Hugh Wood Jan 31 '13 at 16:15
    
Updated the answer :) –  Vardhaman Deshpande Jan 31 '13 at 16:26
    
why should I use ExecuteOrDelayUntilScriptLoaded function if it does not guarantee that the script will be loaded? Can I use SP.SOD.executeFunc function instead of ExecuteOrDelayUntilScriptLoaded any time, in this case I'm sure that the script will be loaded. What purpose of ExecuteOrDelayUntilScriptLoaded using? –  Alexander Jan 31 '13 at 20:27
    
If the script is loaded it runs the function straight away, otherwise it queues the function to be loaded for when the script is available. –  Hugh Wood Feb 1 '13 at 8:48
3  
@Alexander ExecuteOrDelayUntilScriptLoaded only works with scripts which are loaded by default on the page (OnDemand=False). It's sole purpose is to make sure that the function is called only after the script has completed loading. executeFunc works with scripts which are not loaded on the page by default (OnDemand=True). It's job is to load the mentioned script on the page and then call the function. –  Vardhaman Deshpande Feb 1 '13 at 16:54

SP.SOD.executeFunc(key, functionName, fn) method ensures that the specified file (key) that contains the specified function (functionName) is loaded and then runs the specified callback function (fn). Use case for "SP.SOD.executeFunc" can be that at some point you wish a JavaScript library like JQuery be loaded before you call a function defined inside it via callback function.

SP.SOD.executeOrDelayUntilScriptLoaded(func, depScriptFileName) method executes the specified function (func) if the file (depScriptFileName) containing it is loaded; otherwise, adds it to the pending job queue. Use case for "SP.SOD.executeOrDelayUntilScriptLoaded" can be that you want to override a function definition within the Core.js, sample code

function CustomCIMOpt() {
CIMOpt = function (menuElement, title, action, imageSource, sequence, description){
    if(title == 'Compliance Details') return '';
    ULSrLq:;
    var menuOption = CMOpt(title, action, imageSource, sequence, description);
    if(!menuOption) return null;
    for(var i=0; i < menuElement.childNodes.length; i++) {
            var iSeq = menuElement.childNodes[i].getAttribute("sequence");
                if(iSeq)
                if(iSeq > description){
                    menuElement.childNodes[i].parentNode.insertBefore(menuOption, menuElement.childNodes[i]);
                    return menuOption
                }
        }
        AChld(menuElement, menuOption);
        return menuOption;
    };
}

ExecuteOrDelayUntilScriptLoaded(CustomCIMOpt, "core.js");
share|improve this answer
    
Overriding a function definition within the Core.js can be done using both executeFunc as well as executeOrDelayUntilScriptLoaded also. Isnt it? –  variable 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.