Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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");
});
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top