Javascript ajax call on page onload

后端 未结 4 608
庸人自扰
庸人自扰 2020-12-08 20:18

I wish a page to fully load before issuing an ajax call to update database. I can call a javascript function in the body onload event so the page is fully loaded, but I\'m n

4条回答
  •  情书的邮戳
    2020-12-08 20:47

    This is really easy using a JavaScript library, e.g. using jQuery you could write:

    $(document).ready(function(){
    $.ajax({ url: "database/update.html",
            context: document.body,
            success: function(){
               alert("done");
            }});
    });
    

    Without jQuery, the simplest version might be as follows, but it does not account for browser differences or error handling:

    
      
      
      
    
    

    See also:

    • http://docs.jquery.com/How_jQuery_Works#Launching_Code_on_Document_Ready
    • http://api.jquery.com/jQuery.ajax/
    • http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx

提交回复
热议问题