Object Expected – Init.JS on SharePoint 2013 JavaScript and JQuery.


Sathish Nadarajan
SharePoint MVP
Published On :   07 Apr 2014
Visit Count
Today :  1    Total :   18316



In most of the times, we will be using the SP.JS file for getting the client context and do our Client Side Object Model Coding.

Before calling any methods inside the SP.js, we will be using ExecuteOrDelayUntilScriptLoaded method to make sure that the SP.js file loaded properly or not.

The syntax for that would be,

 $(document).ready(function() {
  ExecuteOrDelayUntilScriptLoaded(TempMethod(), "sp.js");
 });
 
 function TempMethod(){
  alert(‘SP.JS Loaded Properly’);
 };
 

This will be fine on our Dev box. But, when we go to the Prod environment, we may not get our code executed. Instead, if you look and debug using the developer tools, it will throw a javascript exception called “Object Expected” on Init.JS.

The reason for this would be, the above syntax calls the Init.debug.js which would have been available on your dev box. But on the production environment, the debug.js may not be there. Then, we cannot include init.debug.js also along with our package.

To eliminate this error,

Just remove the () from the ExecuteOrDelayUntilScriptLoaded Method.

i.e.,

 $(document).ready(function() {
  ExecuteOrDelayUntilScriptLoaded(TempMethod, "sp.js");
 });
 
 function TempMethod(){
  alert(‘SP.JS Loaded Properly’);
 };
 

Though it looks simple, atleast this will reduce 30 mins of a developers effort.

Happy Coding.

Sathish Nadarajan.

Categories