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.