Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint 2013 add javascript after whole page load

Disclaimer: I have no experience with SharePoint2013.

I have problem - I must include/fire some javascript functions after the whole page has been loaded. I tried listening to DOMDocumentReady and window.load events, but sharepoint render the rest of page after those events.

My question is: what I should do to be able to run script after whole page with ajax is rendered. Also I noticed that page navigation is based on hash (#) part. Obviously I must detect that moment also.

Any help or even link to right page in documentation would be great!

like image 263
Kamil Biela Avatar asked Jun 05 '13 09:06

Kamil Biela


People also ask

Can you add JavaScript to SharePoint page?

You can add JavaScript to a page in the same way. Just like with CSS, JavaScript cannot be added by editing the HTML source of the page directly. You have to use the Content Editor web part: Add a Content Editor web part to the page.

Can I run JavaScript before the whole page is loaded?

But yes, it is an option.


1 Answers

You are right, MANY things happen on page after $(document).ready(). 2013 does provide a few options.

1) Script on Demand: (load a js file then execute my code.)

function stuffThatRequiresSP_JS(){     //your code } 

SP.SOD.executeFunc("sp.js")

2) Delay until loaded (wait for a js file, then run)

function stuffToRunAfterSP_JS(){     //your code } ExecuteOrDelayUntilScriptLoaded(stuffToRunAfterSP_JS, "sp.js") 

3) load after other stuff finishes

function runAfterEverythingElse(){     // your code } _spBodyOnLoadFunctionNames.push("runAfterEverythingElse"); 

Sources:

executeFunc: http://msdn.microsoft.com/en-us/library/ff409592(v=office.14).aspx

ExecuteOrDelayUntilScriptLoaded: http://msdn.microsoft.com/en-us/library/ff411788(v=office.14).aspx

Cant find a source on _spBodyOnLoadFunctionNames but I am using it in a few places.

good luck.

like image 122
Will Fawcett Avatar answered Oct 08 '22 19:10

Will Fawcett