Introduction
Hi, I am a SharePoint front-end developer. As such, it is important to understand the client life cycle in terms of DOM, Browser-engine, etc. We will be talking about the Onload process in this post. There are different techniques used to Provide our custom JavaScript code loaded before / in the middle / alter OnLoad events in SharePoint;
Here we will see 8 different techniques used for the 0nload process. Below are the specified Techniques.
Below are the code snippets for each technique in use.
Sys.Application.PageLoad
- Sys.Application.add load(SPLoad);
- function SPLoad(){
- console.log(“Sys.Application.PageLoad.Time:” + ((Date.now()) – performance.timing.navigationStart))
- }
Sys.WebForms.PageRequestManager.PageLoaded
- Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(SPPageLoaded);
- function SPPageLoaded(sender,args){
- console.log(“Sys.Webforms.PageRequestManager.PageLoaded.Time:” + ((Date.now()) – performance.timing.navigationStart))
- }
Document.ready Jquery
- jQuery(document).ready(jqueryLoadsSP);
- function jqueryLoadSP(){
- console.log(“Document.ready Jquery.Time:” + ((Date.now()) – performance.timing.navigationStart))
- }
- function ProcessDefaultOnLoad() {
- ProcessPNGImages();
- UpdateAccessibilityUI();
- UpdateAnimationUserControl(false);
- window.setTimeout('ProcessImn()', 10);
- ProcessOnLoadFunctionNames(_spBodyOnLoadFunctionNames); ProcessOnLoadFunctions(_spBodyOnLoadFunctions);
- if (typeof _spUseDefaultFocus != "undefined")
- DefaultFocus();
- }
_spBodyOnLoadFunctionNames
- _spBodyOnLoadFunctionNames.push('OnPageLoad');
- function OnPageLoad(){
- console.log("_spBodyOnLoadFunctionNames. Time: " + ((Date.now()) - performance.timing.navigationStart));
- }
_spBodyOnLoadFunctions
- _spBodyOnLoadFunctions.push(raiseFunc);
- var raiseFunc = function(){
- console.log("_spBodyOnLoadFunction. Time: " + ((Date.now()) - performance.timing.navigationStart));
- };
ExecuteOrDelayUntilScriptLoaded:sp.core.js
- ExecuteOrDelayUntilScriptLoaded(MyFunction, "sp.core.js");
- function MyFunction(){
- console.log("ExecuteOrDelayUntilScriptLoaded:sp.core.js. Time: " + ((Date.now()) - performance.timing.navigationStart));
- }
SP.SOD.executeFunc: sp.js
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
- function sharePointReady(){
- console.log("SP.SOD.executeFunc: sp.js. Time: " + ((Date.now()) - performance.timing.navigationStart));
- }
ExecuteOrDelayUntilBodyLoaded
- ExecuteOrDelayUntilBodyLoaded(delayBody);
- function delayBody(){
- console.log("ExecuteOrDelayUntilBodyLoaded. Time from NavStart: " + ((Date.now()) - performance.timing.navigationStart));
- }
Output
Chrome
Run 1
Run 2
Run 3
IE11
Run 1
Run 2
Run 3
As you see the above images of the output of the code snippet run on both chrome and IE11 browsers, here we discover the sequence of execution.
We will talk about both the sequences,
Order | Chrome | IE11 |
1 | ExecuteOrDelayUntilBodyLoaded | document.ready Jquery |
2 | Sys.Application.PageLoad. | ExecuteOrDelayUntilBodyLoaded |
3 | document.ready Jquery | Sys.Application.PageLoad. |
4 | SP.SOD.executeFunc: sp.js. | SP.SOD.executeFunc: sp.js. |
5 | _spBodyOnLoadFunctionNames | _spBodyOnLoadFunctionNames |
6 | _spBodyOnLoadFunction | _spBodyOnLoadFunction |
7 | ExecuteOrDelayUntilScriptLoaded:sp.core.js | ExecuteOrDelayUntilScriptLoaded:sp.core.js |
8 | Sys.WebForms.PageRequestManager.PageLoaded | Sys.WebForms.PageRequestManager.PageLoaded |
- ExecuteOrDelayUntilBodyLoaded function is always executed first in chrome (but at this stage we cannot access SP methods). Whereas the document.ready Jquery function is executed first in IE11.
- This could be useful to execute our custom code at a really early stage in the OnLoad process keeping in mind the order of execution.
- There are two SharePoint onLoad functions _spBodyOnLoadFunctionNames and _spBodyOnLoadFunction. Always executed in the order. SO, if we want to execute some code after all functions included by us (or other devs) in _spBodyOnLoadFunctionNames, then it is useful to use this one _spBodyOnLoadFunction, because is executed the last.
- If we want to execute some functions after all functions (SP, after load functions, Yammer, etc.) we can use this function to attach the OnLoad event -> Sys.WebForms.PageRequestManager.PageLoaded.
- I have referred to an article to implement this particular functionality, check the URL.
No comments:
Post a Comment