javascript - Ajax isn't working on website -
for personal site want insert simple ajax server clock website, reason it's not showing in header. here's javascript code
var httpxml; try { // firefox, opera 8.0+, safari httpxml=new xmlhttprequest(); } catch (e) { // internet explorer try { httpxml=new activexobject("msxml2.xmlhttp"); } catch (e) { try { httpxml=new activexobject("microsoft.xmlhttp"); } catch (e) { alert("ur browser doesn't support ajax m8, try reinstalling windows 95"); return false; } } } function stateck() { if(httpxml.readystate==4) { document.getelementbyid("time").innerhtml=httpxml.responsetext; } } var url="ajax-server-clock-demock.php"; url=url+"?sid="+math.random(); httpxml.onreadystatechange=stateck; httpxml.open("get",url,true); httpxml.send(null); tt=timer_function(); } function timer_function(){ var refresh=1000; // refresh rate in milli seconds mytime=settimeout('ajaxfunction();',refresh) }
and in php file
<?php echo date("d/m/y : h:i:s", time()); ?>
lastly in header
<time>bacon</time>
i have see more code in context because several things can make not work, in mean time, there multiple issues script, among others:
you did settimeout(). execute once, assuming want clock update every second need use:
http://www.w3schools.com/jsref/met_win_setinterval.asp
oh, also, need like:
document.getelementsbytagname('time')[0];
instead of getelementbyid()
or add id element like:
<time id="mytime"></time>
and can call:
document.getelementbyid('mytime');
got it?
and also, declared ajaxfunction() calling in settimeout?
also, there better ways clock... , should consider using jquery or that, make life easier when doing ajax, dom manipulation , etc...
Comments
Post a Comment