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.
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!
@AshikPaul indeed, it complains about “Uncaught ReferenceError: $ is not defined”
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?
Yes, to use the $ functions for JQuery you need to use JQuery Script from JQuery.com and also place the code inside tags