页面加载时的 Javascript ajax 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4144768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:04:55  来源:igfitidea点击:

Javascript ajax call on page onload

javascriptajax

提问by Hector Barbossa

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 not sure how to trigger the Ajax call from there.Is there any better way of achieiving this (Upadating database after page load)?

我希望在发出 ajax 调用更新数据库之前完全加载页面。我可以在主体 onload 事件中调用一个 javascript 函数,以便页面完全加载,但我不确定如何从那里触发 Ajax 调用。有没有更好的方法来实现这一点(页面加载后更新数据库)?

回答by Eero

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

使用 JavaScript 库这真的很容易,例如使用 jQuery 您可以编写:

$(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:

如果没有 jQuery,最简单的版本可能如下,但它不考虑浏览器差异或错误处理:

<html>
  <body onload="updateDB();">
  </body>
  <script language="javascript">
    function updateDB() {
      var xhr = new XMLHttpRequest();
      xhr.open("POST", "database/update.html", true);
      xhr.send(null);
      /* ignore result */
    }
  </script>
</html>

See also:

也可以看看:

回答by Trecenti

You can use jQuery to do that for you.

您可以使用 jQuery 为您做到这一点。

 $(document).ready(function() {
   // put Ajax here.
 });

Check it here:

在这里检查:

http://docs.jquery.com/Tutorials:Introducing_%24%28document%29.ready%28%29

http://docs.jquery.com/Tutorials:Introducing_%24%28document%29.ready%28%29

回答by JST

Or with Prototype:

或者使用原型:

Event.observe(this, 'load', function() { new Ajax.Request(... ) );

Or better, define the function elsewhere rather than inline, then:

或者更好的是,在别处而不是内联定义函数,然后:

Event.observe(this, 'load', functionName );

You don't have to use jQuery or Prototype specifically, but I hope you're using some kind of library. Either library is going to handle the event handling in a more consistent manner than onload, and of course is going to make it much easier to process the Ajax call. If you must use the body onload attribute, then you should just be able to call the same function as referenced in these examples (onload="javascript:functionName();").

您不必专门使用 jQuery 或 Prototype,但我希望您使用的是某种库。任何一个库都将以比 onload 更一致的方式处理事件,当然也会更容易处理 Ajax 调用。如果您必须使用 body onload 属性,那么您应该能够调用与这些示例 ( onload="javascript:functionName();") 中引用的相同的函数。

However, if your database update doesn't depend on the rendering on the page, why wait until it's fully loaded? You could just include a call to the Ajax-calling function at the end of the JavaScript on the page, which should give nearly the same effect.

但是,如果您的数据库更新不依赖于页面上的渲染,为什么要等到它完全加载?您可以在页面上 JavaScript 的末尾包含对 Ajax 调用函数的调用,这应该会产生几乎相同的效果。

回答by Chris Broski

It's even easier to do without a library

没有图书馆就更容易了

window.onload = function() {
    // code
};