Ajax xml httprequest to jquery conversion
I'm pretty new to javascript / ajaax / jquery, but I was able to figure out the following ajax script. Unfortunately it doesn't work cross browser... as far as I can tell, not in any IE versions... I'm wondering if anyone can help me out with how to convert it to jquery? I heard that is way better than using ajax.
<script type="text/javascript">
function checkRefresh(str)
{
if (str=="") {
document.getElementById("lastCallID").innerHTML="";
return;
}
if (window.XMLHttpReques开发者_JAVA技巧t) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
if (document.getElementById("lastCallID").innerHTML < xmlhttp.responseText) {
GoPopUp();
} else {
setTimeout('checkRefresh()',15000)
}
}
}
xmlhttp.open("GET","getnewid.php",true);
xmlhttp.send();
}
update: I have the following code. But I still can't get it to work. I want this ajax request to be sent every 15 seconds... It calls this function getnewid.php and that function echo's out an ID... I then want the ajax to compare the id it got from getnewid.php with the "LastCallID" if the new ID is greater I want it to call this function GoPopUp. If the new ID isn't greater I want it to call itself. I initialzed this ajax request by doing
<script type="text/javascript">
$.ajax({ url: "getnewid.php", success: function(data) { if (document.getElementById("lastCallID").innerHTML < data) { GoPopUp();
} else { setTimeout('$.ajax()',15000) } } });EDIT: @Ben, this is how I've implemented it. (For what ever reason I can't get all the code to show up, please see attached screenshot) http://www.screencast.com/users/bibbles10504/folders/Jing/media/7e631bd9-e9df-4cb9-ab4f-a7672bbea0a0
<head>
.......
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
initialize();
showClock();
(function checkRefresh() {
$.ajax({
url: 'getnewid.php',
success: function(data) {
if (parseInt($('#lastCallID').html()) < parseInt(data)) GoPopUp();
else setTimeout(checkRefresh, 15000);
}
});
})();
});
</script>
.......
</head>
<body>
.......
</body>
You should use jquery for ajax $.ajax, it takes care of all that cross browser stuff for you.
http://api.jquery.com/jQuery.ajax/
精彩评论