<script type="text/javascript">
var t;
function tick() {
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200 && document.getElementById("txtHint").innerHTML!=xmlhttp.responseText) {
doc开发者_如何学Goument.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open("GET","http://ubarskit.com/v/response.php",true);
xmlhttp.send();
t=setTimeout("tick()",1000);
}
</script>
</head>
<body onload="tick()">
<span id="txtHint"></span>
</body>
I want it to update the text in the span-tag from response.php every second. Works in chrome, but not in Firefox or Opera.
Here's the response.php file:
<?php
if(isset($_GET['ropa']))
{
$fp = fopen('shout.txt', w);
fwrite($fp, $_GET['ropa']);
fclose($fp);
}
else
{
echo file_get_contents('shout.txt');
}
?>
I would verify that the status is what you expect by adding a bit of debug output. If it is returning status 0, you probably have a cross-domain security issue.
<script type="text/javascript">
var t;
function tick() {
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
document.getElementById("debug").innerHTML = "readyState: " + xmlhttp.readyState + "<br />status: " + xmlhttp.status;
if (xmlhttp.readyState==4 && xmlhttp.status==200 && document.getElementById("txtHint").innerHTML!=xmlhttp.responseText) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open("GET","http://ubarskit.com/v/response.php",true);
xmlhttp.send();
t=setTimeout("tick()",1000);
}
</script>
</head>
<body onload="tick()">
<span id="debug"></span>
<span id="txtHint"></span>
</body>
Instead of doing it like real men, I used jQuery ;)
<script type="text/javascript">
function tick() {
$.get('response.php',function(data) {
$('#txtHint').html(data);
});
}
function submitShout(form){
$.get('response.php',{ ropa: form.elements['ropa'].value});
}
</script>
</head>
<body onload="window.setInterval('tick()',1000)">
<span id="txtHint"></span>
<form action="response.php" method="get" onsubmit="submitShout(this); return false;">
<input type="text" id="ropa" name="ropa">
</form>
精彩评论