JavaScript not running on Published pages


Hello everyone,

Today I will talk about a problem concerning JavaScript.

Recently I had to integrate a script to one of my pages and I’ve realized that the code works correctly while the page status was not « Published ».

The code was:

$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(MyCustomFunction, “sp.js”); });

After doing some research, I came across this post msdn with an example of JavaScript use in SharePoint 2013 :

So use the function SP.SOD.executeFunc() ; that will load the required JavaScript and not ExecuteOrDelayUntilScriptLoaded() to make sure JavaScript is loaded, the script is loaded only if it is explicitly required.

The correct code is:

$(document).ready(function () { SP.SOD.executeFunc(‘sp.js’, ‘SP.ClientContext’, MyCustomFunction); });

Hope this helps.





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s