3

Share experience

As recommended in various posts I changed the way the functions load from

$(document).ready(function() { 
   functionName();
});

to

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', functionname);

Works great with the following expection:

I have several pages where several webparts each load a separate SP.SOD.executeFunc(). One webpart loads a SP.SOD.executeFunc() that creates a JumboTron then below it a different webpart loads three SP.SOD.executeFunc()s to create three columns of output.

Not a problem for Chrome but it appears that IE 11 does not like SP.SOD.executeFunc() loads spanning webparts as the Jumbotron loads but the three columns fails - no error just does not excute. I had to create a new webpart that loaded all the SP.SOD.executeFunc()s in order to work in IE 11.

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', functionjumbotron);
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', functioncolumn1);
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', functioncolumn2);
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', functioncolumn3);

Question is there another way to load these so it can span webparts?

4
  • Have you tried it?
    – Hardik
    Commented Jun 7, 2016 at 20:28
  • Please update about the resolution if you get it.
    – Hardik
    Commented Jun 8, 2016 at 14:35
  • Yes I placed all the SP.SOD in a single webpart at the bottom of the page.
    – PianoMan
    Commented Jun 8, 2016 at 15:28
  • Is that works for you?
    – Hardik
    Commented Jun 8, 2016 at 15:33

1 Answer 1

3

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.

OR you can use below

SP.SOD.executeOrDelayUntilScriptLoaded(someFunction,"sp.js");

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);
8
  • Did you get it working? Did my answer helps you?
    – Hardik
    Commented Jun 7, 2016 at 17:40
  • Sorry for the delay. You were a big help as I was able to get it to run.
    – PianoMan
    Commented Jun 8, 2016 at 17:08
  • So kindly mark it so others can get benefit of it.
    – Hardik
    Commented Jun 8, 2016 at 17:09
  • As well as give some credit to my answer by upvoting it.
    – Hardik
    Commented Jun 8, 2016 at 17:20
  • Please bare with me as I'm new to this
    – PianoMan
    Commented Jun 8, 2016 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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