20

I currently have a JavaScript file that gets loaded in my SharePoint pages.

jQuery(document).ready(function() {
 ExecuteOrDelayUntilScriptLoaded(myfunction(), "sp.js");
});

myfunction = function(){
 /*Do something here.*/
};

Now the JavaScript runs just fine, however on a list page, it prevents the ribbon from loading the List tab. In the console I get below error.

SCRIPT5007: Object expected init.debug.js, line 5153 character 5 error.

Even if I comment out all of the function so it looks exactly like above I still get the error message.

I have noticed that if I change "sp.js" to "sp.debug.js" I do not get the error message. From looking at the scripts that are loaded on the page, this makes sense as sp.debug.js is loaded and not sp.js. I don't really want to put sp.debug.js in the javascript code as when it reaches production servers, they won't be using sp.debug.js. I've never notice this happen before with any code that has ExecuteOrDelayUntilScriptLoaded(jsfunction(), "sp.js"). Is there a way to get it to work for both environments like with a #ifdebug statement in javascript? Or some other idea.

2 Answers 2

32

You are using ExecuteOrDelayUntilScriptLoaded wrong, you should use the function name, not execute it.

Like this:

jQuery(document).ready(function() {
 ExecuteOrDelayUntilScriptLoaded(myfunction, "sp.js");
});

Note that i removed the "()" from myfunction. Now it should work as expected

0
-1

Do not use jQuery for such purposes, use SharePoint embedded features instead:

var myFunction = function(){
    ExecuteOrDelayUntilScriptLoaded(yourfunction, "sp.js");
}
_spBodyOnLoadFunctionNames.push("myFunction");

or an anonymous function

_spBodyOnLoadFunctionNames.push(function(){
    ExecuteOrDelayUntilScriptLoaded(yourfunction, "sp.js");
});
2
  • This will not work. You have to use ExecuteOrDelayUntilScriptLoaded() because _spBodyOnLoad is buggy. Also, the fact that the code is in a jQuery wrapper is irrelevant.
    – TylerH
    Aug 29, 2018 at 13:51
  • 1
    Well, in my code _spBodyOnLoadFunctionNames works fine. ExecuteOrDelayUntilScriptLoaded() is needed only if you use CSOM, otherwise it isn't, in fact the user did not specify he uses it. So why the downvote?
    – Emaborsa
    Aug 30, 2018 at 8:12

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