开发者

ajax not working in IE

I'm having this problem with my AJAX, where it's not working in IE, but it is working in FF. I got a majority of the following code off the w3schools website, so I don't see why it would be wrong. Can anyone help me out?

<script type="text/javascript">
    function checkRefresh(str)
    {
        if (str=="") {
            document.getElementById("lastCallID").innerHTML="";
            return;
        }

        if (window.XMLHttpRequest) {
            // 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();
    }
</script>

THANKS!

EDIT: As far as I can tell, it's not working in IE6 -8. Basically what the idea is, is that the ajax calls a script which runs a php page which gets the most recent ID in a database. Then it compares the ID from the php page, to the ID on the main page. If the ID from the Ajax request is greater than what is on the page, then it starts a function which starts a jquery popup to alert the user that there is a new database entry and gives them the option to refresh. in IE 6,7,8 That popup never starts, so I can only imagine that the ajax is not working correctly to grab the new ID. The refresh function works when I call it, that's why I think i开发者_StackOverflow中文版t's the ajax that's the issue. But in case you're curious, here is the GoPopUp function

var GoPopUp = function(){
            $('#PopNewCall').fadeIn('slow');
            PageRefreshTimer();

        }

        //Function which refreshes page after a certain number of seconds with no user Inputs
        var PageRefreshTimer = function(){
            setTimeout("location.reload(true);",30000); //1,000 = 1 second
        }

        //Function which refreshes page after user has clicked refresh
        var RefreshNow = function(){
            setTimeout("location.reload(true);",0);
        }


Here's a function I like to use for getting an XMLHttpRequest object cross browser.

function getRequestObject() {
    var options = [
        function () { return new XMLHttpRequest() },
        function () { return new ActiveXObject("Microsoft.XMLHTTP") },
        function () { return new ActiveXObject("Msxml2.XMLHTTP.6.0") }
    ];

    for (var i = 0, il = options.length; i < il; i++) {
        try { return options[i]() } catch(e) {}
    }
}

var xhr = getRequestObject();
if (!xhr) { return; } // :(

// use xhr here
xhr.open("GET", "/foo/bar");


For a simple cross-browser solution, I would suggest using jQuery.
jQuery AJAX API can be found at http://api.jquery.com/jQuery.ajax/


i don't see the failure in your code, but: is there any reason to not use one of the great, existing frameworks like twobirds, jquery, mootools or prototypejs to do ajax-stuff? if you try to write this on your own, supporting all browsers (or at least the big ones) you'll get to hell and spend a lot of time (time that you could use to develop or refine other things)

EDIT: as answer to your comment: i don't know what exactrly you're trying to do, but it looks like you're periodically repfeshing 'getnewid.php' to 'lastCallID'. it would be like this (don't know what 'GoPopUp()' is for, just calling it after every update in this case):

var refreshInterval = setInterval(function() {
     $('#lastCallID').load('getnewid.php', function(){ GoPopUp(); });
}, 15000);


Edit: Sorry, I would of posted this as a comment to Catie's reply, but as a new user I do not have sufficient 'reputation' points. Despite that, I'm sure most will agree with the validity of my comment.


@catie, As developers we all have our issues with IE, but it would seem odd for a teacher to instruct students "not to bother with IE" as this would do little to prepare you for the real world.

The reality is IE is not going away anytime soon and between versions 6/7/8 they still have over 50% of the market share globally. As a best practice it is always better to ensure your site/code works in all browsers as expected, much like @august solution / suggestion.


Do you have to submit your assignment on IE? Our teacher (when I took the relevant similar course) asked us to not bother with IE as a lot of things don't work on it. Try refreshing your browser or something.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜