{"id":8357,"date":"2015-10-05T12:20:56","date_gmt":"2015-10-05T12:20:56","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/key-points-to-know-when-using-sp-sod-executefunc-and-sp-sod-executeordelayuntilscriptloaded\/"},"modified":"2016-07-28T10:57:04","modified_gmt":"2016-07-28T10:57:04","slug":"key-points-to-know-when-using-sp-sod-executefunc-and-sp-sod-executeordelayuntilscriptloaded","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/key-points-to-know-when-using-sp-sod-executefunc-and-sp-sod-executeordelayuntilscriptloaded\/","title":{"rendered":"Key Points to Know When Using SP.SOD.executeFunc and SP.SOD.executeOrDelayUntilScriptLoaded"},"content":{"rendered":"<p>So there I was trying to get SharePoint&#8217;s script on demand (SOD) working correctly and I thought I had it all figured out.&#160; I ran across a post called <a href=\"http:\/\/www.ilovesharepoint.com\/2010\/08\/sharepoint-scripts-on-demand-spsod.html\">SharePoint Scripts on Demand<\/a> which covers the topic very thoroughly, but there is some behavior between <strong>executeOrDelayUntilScriptLoaded<\/strong> and <strong>executeFunc<\/strong> that I don&#8217;t think is really clearly called out. That lack of understanding led to some major frustrations, and I would like to alleviate you of that fate.&#160; So here&#8217;s the deal:<\/p>\n<p>The <strong>executeOrDelayUntilScriptLoaded<\/strong> function accepts a function delegate and a script key.&#160; If the script associated with the key is loaded, then the function delegate executes immediately.&#160; If the script is not loaded, the function delegate is queued up and will execute if and when the script associated with the script key is ever loaded.&#160; Understand, however, that the script may never be loaded so that queued method delegate may never fire.&#160; I think most people tend to understand how this function operates fairly well.<\/p>\n<p>The <strong>executeFunc<\/strong> function, however, does not work like I would expect it to work.&#160; This function accepts a script key, a JavaScript AJAX class type name (normally this is just null), and a function.&#160; So it&#8217;s very similar to the <strong>executeOrDelayUntilScriptLoaded<\/strong> function, but the key different is that if the script associated with the script key is not loaded, <strong>executeFunc<\/strong> loads the script and executes the delegate method.&#160; This is fine and good and exactly what I would expect.&#160; What I believe to be odd is that if the script associated with the script key is already loaded, then <strong>executeFunc <\/strong>does absolutely <strong>NOTHING<\/strong>.&#160; I assumed it would run the delegate method if the script was already loaded, but that is apparently not the case.<\/p>\n<p>What does this mean?&#160; Well, in my case I was trying to load the scripts on demand when a user clicked a ribbon button and then execute a method in my script.&#160; My initial attempt at this just used the executeFunc method.&#160; Unfortunately, that meant that my code worked the first time the button was clicked, but never again.&#160; To get it to work, I had to use both methods in conjunction with one another as follows:<\/p>\n<p> <code>   <\/code><\/p>\n<p><code>executeOrDelayUntilScriptLoaded(function()      <br \/>&#160; { ... code ... }, \"myScript.js\");       <br \/>executeFunc(\"myScript.js\", null, null);<\/code><\/p>\n<p><code> <\/code>  <\/p>\n<p>The call to <strong>executeOrDelayUntilScriptLoaded<\/strong> contains the code I actually want to run.&#160; The first time through, the function gets queued up because my script is not loaded.&#160; The next line contains the <strong>executeFunc <\/strong>method that ensures my script is loaded.&#160; Notice that the delegate method is null.&#160; Once the script is loaded, my button click code executes because that queued function gets fired.&#160; The second time through, my button click code executes immediately because the <strong>executeOrDelayUntilScriptLoaded<\/strong> sees that my script is already loaded, and the <strong>executeFunc<\/strong> line does nothing because the script is already loaded.&#160; So it takes two methods to get the job done, but it seems to work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So there I was trying to get SharePoint&#8217;s script on demand (SOD) working correctly and I thought I had it all figured out.&#160; I ran across a post called SharePoint Scripts on Demand which covers the topic very thoroughly, but there is some behavior between executeOrDelayUntilScriptLoaded and executeFunc that I don&#8217;t think is really clearly&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-8357","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=8357"}],"version-history":[{"count":4,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8357\/revisions"}],"predecessor-version":[{"id":42513,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8357\/revisions\/42513"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=8357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=8357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=8357"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=8357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}