Tell me more ×
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

2 Answers

up vote 2 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>

If you are using SP2013 then you will want to look into jquery.livequery which is a plugin.

This will allow you to look for changes that happen on the page, dynamically applying styles to newly created elements.

These are my thus far best practices for loading and using scripts.

share|improve this answer
2  
you can find this post more helpful. – Alexander Feb 4 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 at 11:11

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 at 16:15
Updated the answer :) – Vardhaman Deshpande Jan 31 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 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 at 8:48
@HughWood, the main reason of this question is if I use ExecuteOrDelayUntilScriptLoaded function and does not use SP.SOD.executeFunc (or other function that loads js file), the code in ExecuteOrDelayUntilScriptLoaded block is never invoked. Thus I would like to know where should I use function for load and function for delay execution. What the best practice? – Alexander Feb 1 at 11:40
show 2 more comments

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.