我希望在发出更新数据库的ajax调用之前完全加载页面.我可以在body onload事件中调用javascript函数,以便页面完全加载,但是我不知道如何从那里触发Ajax调用.有没有更好的方法来实现这一点(页面加载后的Upadating数据库)?
Eer*_*ero 46
使用JavaScript库非常简单,例如使用你可以编写的jQuery:
$(document).ready(function(){
$.ajax({ url: "database/update.html",
context: document.body,
success: function(){
alert("done");
}});
});
Run Code Online (Sandbox Code Playgroud)
没有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>
Run Code Online (Sandbox Code Playgroud)
也可以看看:
小智 6
您可以使用jQuery为您执行此操作.
$(document).ready(function() {
// put Ajax here.
});
Run Code Online (Sandbox Code Playgroud)
在这里查看:
http://docs.jquery.com/Tutorials:Introducing_%24%28document%29.ready%28%29