简体   繁体   English

在后台加载页面?

[英]Load page in background?

I have following code embedded into a webpage which is intended to send the clients current time to the server:我在网页中嵌入了以下代码,旨在将客户端当前时间发送到服务器:

<script type="text/javascript">
function settime() 
{
   var d = new Date();
   var h = d.getHours();
   var m = d.getMinutes();
   $.get('settime.html?h='+h+'&m='+m, function(data, status)
   {
   });
}
</script>
 <body onload="settime();">
 ...

The idea: on loading of the body, JS-function settime() should be called which itself accesses the page "settime.html" with some parameters handed over.想法:在加载主体时,应该调用 JS 函数 settime(),它本身访问页面“settime.html”并传递一些参数。

The problem: settime.html is not called, there is never such a request at the server.问题: settime.html 没有被调用,服务器上从来没有这样的请求。 Any idea what could be wrong here?知道这里有什么问题吗?

Thanks!谢谢!

You can use您可以使用

$(document).ready(function() {

       var d = new Date();
       var h = d.getHours();
       var m = d.getMinutes();
       $.get('settime.html?h='+h+'&m='+m, function(data, status)
       {
         //Handle Success Here
       });

});

This way as soon as the DOM has loaded Jquery will fire the .ready function which will run your code.这样,只要 DOM 加载 Jquery 就会触发.ready function ,它将运行您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM