3

Environment: SP 2013 on prem, team site with publishing feature enabled

I want to call my javascript after the complete page is loaded. The page has some webparts that have some rest calls. So I want to run my javascript after all those calls are finished.

I tried pushing my script with

  1. _spBodyOnLoadFunctionNames.push
  2. window.attachEvent("onload", mycustom function);
  3. Script link defer =true, load after ui= true

But nothing worked. My script is running and afterwards the rest calls are finishing.

2
  • Try SP.SOD.executeFunc('sp.js', 'SP.ClientContext', yourCustomFunction);. Nov 6, 2015 at 10:31
  • Do you know the count of how may rest calls are there in the webparts and can you add some code on the REST call success handler?
    – Unnie
    Nov 6, 2015 at 11:03

4 Answers 4

6

The whole essence of async REST calls is that they do not stall other code.

SOD won't be of help because it has no clue you are waiting for other operations to finish

Best thing is to handle those REST operations properly: https://stackoverflow.com/questions/3709597/wait-until-all-jquery-ajax-requests-are-done

If you have no control over that code your only option is to wait for a period of time and (hope) all REST calls have been processed by then.

SharePoint gives you an _spYield( function , cycles ) wrapper around the setTimeout function which you can use to defer execution of a function.

If you know what that REST outcome is going to be, you can check for it in that spYield function every 100 cycles or so and continue with your code when you think everything is done.

0

Include the JQuery library and then enclose your javascript in $(document).ready(function(){[your javascript]});

0

Danny's answer is good but downplays the situation where you have no control over the other code and you must add a waiting period (understandable, and not 'proper', but it is sometimes necessary).

I successfully did this by running a check every 100ms for the data in a REST-called Kanban I needed to run the next script. If the data was present on the page, it fires the next function.

$(document).ready(function() { 
    function doSomething() {
        //function to run
    }
    function runAfterKanban(){
        setTimeout( function() {
            if ($(".itemClassToLookFor").length > 0) {
                doSomething();
            } else {
                runAfterKanban() //try again in 100ms
            }
        }, 100)
    }
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(runAfterKanban); //essentially as 'last' as you can get
});
-1

Try using

ExecuteOrDelayUntilScriptLoaded(InitializeRESTCall, "sp.js"); // as needed 
_spBodyOnLoadFunctionNames.push("functionAfterREST");

The spBodyOnLoadFunctionNames should fire function mentioned in argument after the call above. The InitializeRESTCall will contain the actual functions to call the web service and manipulate data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.