Search code examples
javascriptjqueryjspgetjsondocument-ready

Can we add muliple document.ready functions in a jsp page with unique API calls inside them?


When I added multiple document.ready functions in a jsp page with unique API calls inside each of the document.ready function I see a error in the google chrome console like follows. And the relevent data is not loaded to the web page.

:8080/myurl/restcall Failed to load resource: the server responded with a status of 500 ()


Solution

  • Yes you can have multiple callback function on document.ready

    like this

    $( document ).ready(function() {
        console.log( "ready!" );
    });
    $( document ).ready(function() {
        console.log( "log!" );
    });
    $( document ).ready(function() {
        console.log( "status!" );
    });
    

    this have nothing to do with your api response

    but you can not have multiple callback function on window.onload like this

    window.onload = function() {
      console.log("hello window")
    };
    
    window.onload = function() {
      console.log("hello window again")
    }
    

    because it will overweight the previous one