简体   繁体   中英

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.

The problem: settime.html is not called, there is never such a request at the server. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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