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

Sathish Nadarajan
 
Solution Architect
April 7, 2014
 
Rate this article
 
Views
25216

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment