Thursday, 28 May 2015

Execute Javascript or JQuery after the Entire page has loaded in SharePoint

If you're anything like me and are working with a lot of Javascript/JQuery within Sharepoint. You will eventually need to run a script after the page loads. So you could add defer="defer" to your script tag, which sometimes works and sometimes doesn't.

You might be tempted to window.onload = yourFunction(). But this will break the way Sharepoint load's it's pages. As Sharepoint also needs to do a lot of things after the page loads.

So the solution is:

Javascript:
window.attachEvent("onload", yourFunction);

So instead of overriding the onload event. You are now adding an event to the queue. Simply replace the "yourFunction" bit with your function name.

Eg:

window.attachEvent("onload", HideBranding);

function HideBranding() {
.....
.....
}

JQuery:

$(window).load(function() {
....
....
});