none
Execute a JavaScript code after the entire SharePoint master page loads RRS feed

  • Question

  • Hi
    I need to execute a javascript after the entire page has loaded in sharepoint and the DOM content is available. I can't find how I should do that, as even Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded is still too early.

    Thanks & Best Regards,

    Daryush


    Saturday, May 16, 2020 1:37 PM

Answers

  • Hi,

    Finally I found the correct  answer in Stack Overflow Forum  below link :

    Correct answer

    in body tag I wrote:

    <body onload="myFunction()">

    And at the end of page before </body> I wrote my javaScript

      <script type="text/javascript">
    function myFunction(){ .......

    Thanks & Best Regards,

    Daryush


    Friday, May 29, 2020 5:49 PM

All replies

  • I would say the easiest way is to add your javascript funtion to "_spBodyOnLoadFunctionNames" array:

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



    Saturday, May 16, 2020 9:29 PM
  • Hi Maxim,

    The problem with 

    _spBodyOnLoadFunctionNames.push("yourFunc");

    is that it does not work at all - the function is never called. I'm using Sharepoint 2016

    Sunday, May 17, 2020 4:44 PM
  • 1) First: (load a js file then execute below 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");

    Below article for your reference:

    https://stackoverflow.com/questions/16936250/sharepoint-2013-add-javascript-after-whole-page-load


    sharath aluri

    Monday, May 18, 2020 1:51 AM
  • Hi,

    May I know how you execute the script?

    I tested in webpart page and publishing page, works fine.

    <script>
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(SPPageLoaded);  
    function SPPageLoaded(sender,args){  
        alert("Sys.Webforms.PageRequestManager.PageLoaded.Time:" + Date.now());
    } 
    </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, May 18, 2020 2:39 AM
  • Hi Sharath

    Thanks for your solutions. Today I,v worked on another job. Tomorrow I 'll use your solutions

    Thanks & Best Regards

    Daryush

    Monday, May 18, 2020 11:46 PM
  • Hi,

    Thanks for your reply. I'll use your solution tomorrow and if I have problem I 'll ask you 


    Thanks & Best Regards

    Daryush

    Monday, May 18, 2020 11:55 PM
  • Hi Lee

    Thanks for your reply. I used your code in mater page seeattle.master  in SharePoint designer I got a picture of using your code  below :

    But it is not I want it load before everything I want it load after all content of page

    Thanks & Best Regards

    Daryush

     

    Tuesday, May 19, 2020 11:08 AM
  • Hi,

    As you see, when the debugger gets triggered, the page elements and SharePoint libraries are loaded.

    Anyway, you could setTimeout to postpone the logic you want to execute.

    <script>
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(SPPageLoaded);  
    function SPPageLoaded(sender,args){ 
    //3 seconds
    setTimeout(function(){ alert("Sys.Webforms.PageRequestManager.PageLoaded.Time:" + Date.now()); }, 3000); 
        
    } 
    </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, May 20, 2020 2:18 AM
  • Hi,

    Thanks for your time, I need it in master page I need this script to translate date to Persian date. Is your script working in master page?

    Thanks for your reply & Best Regards

    Daryush 

    Thursday, May 21, 2020 4:20 PM
  • Hi,

    The script insert into master page directly(same as you shared).

    You may try to test in a new site(no customization) to confirm whether this related to other customization in the site.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, May 22, 2020 12:54 AM
  • Hi,

    Thanks for your reply. I will use in a new site  but I didn't costume in this site 

    I 'll give you the result.

    Thanks & Best Regards 

    Daryush

    Saturday, May 23, 2020 1:43 PM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can mark the helpful post as answer to help other community members find the helpful information quickly.

    You can also share your own solution here and mark it as answer, and we can learn from each other.


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, May 27, 2020 6:48 AM
  • Hi,

    Thank you for your reply, I was sick few days so I couldn't check. Now I am checking and I 'll tell you with a new site .

    Thanks & Best Regards,

    Daryush

    Wednesday, May 27, 2020 4:29 PM
  • Hi,

    I checked your solution it does not work 

    My job does not solved with timer I need to translate date to Persian date with JavaScript  one time there is just one record one time maybe page has tens row and a lot of column with date field this job will not be solved with 3 or 10 seconds 

    Thanks & Best Regards,

    Daryush

    Friday, May 29, 2020 3:40 PM
  • Hi,

    Finally I found the correct  answer in Stack Overflow Forum  below link :

    Correct answer

    in body tag I wrote:

    <body onload="myFunction()">

    And at the end of page before </body> I wrote my javaScript

      <script type="text/javascript">
    function myFunction(){ .......

    Thanks & Best Regards,

    Daryush


    Friday, May 29, 2020 5:49 PM