Solution 1 :

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.

Problem :

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!

Comments

Comment posted by Ashik Paul

Did you check for any errors in the console?

Comment posted by Elmi

@AshikPaul indeed, it complains about “Uncaught ReferenceError: $ is not defined”

Comment posted by Elmi

When using this code the console gives me an error “Uncaught ReferenceError: $ is not defined”. Shouldn’t I place this inside some script-tags outside of the body?

Comment posted by CADSolutionsUK

Yes, to use the $ functions for JQuery you need to use JQuery Script from JQuery.com and also place the code inside tags

By