Last time, my team has worked with javascript on SharePoint page

There are a inject javascript on the page with a Script editor webpart. We did a try to run a function after page load with a common jquery fuction

$(document).ready()

As our expect, code included inside $( document ).ready() will run once the page Document Object Model (DOM) is ready for JavaScript code to execute. More detail about this function you can find more at http://learn.jquery.com/using-jquery-core/document-ready/

But our problem is the code was not execute as normal. So we work around on this

$(document).ready(function() {

setTimeout(function(){ execute code here},3000);

});

It works but it was not a good solution.

After research, I cannot remember exactly where, but finally I belive below code is the best solution for this issue.

SP.SOD.executeFunc(‘sp.js’, null, function(){
// Execute code here
})

 

Cheers

Hoang Nhut NGUYEN