开发者

javascript acting weird in chrome

开发者 https://www.devze.com 2023-02-01 12:15 出处:网络
var check=httpReq.readyState==4?(httpReq.status==200?true:false):false; alert(check); in firefox it just pops up true while in chrome it pops 开发者_运维百科up twice false and true respectively.
var check=httpReq.readyState==4?(httpReq.status==200?true:false):false;
alert(check);

in firefox it just pops up true while in chrome it pops 开发者_运维百科up twice false and true respectively. ???

many thanks


The "onreadystatechange" callback may be called once or it may be called a zillion times (really, a zillion). Your "alert" call happens regardless of the value of "check".


This code must be being executed multiple times there's no way a single call to alert caused three dialogs to pop-up without the context of this call it's impossible to answer. What if you change alert(check) to alert("hello world") does it still happen three times in chrome and one time in fire fox if so you know there's nothing wrong with your ternary expression and the problem lies in the calls to whatever this code is wrapped in rather than anything wrong with the expression itself. The way events are handled and dispatched by browsers varies so it could be if this is in some event handler that the event is dispatched multiple times by chrome vs firefox, varying interpretation of the specification causes these types of discrepancies between browser behavior (the reason I got into and have stuck with Flex, at least the browser compatibility issues are on a single entity, Adobe, this way and there's someone to blame and file a bug report with).


Why would this be surprising? I assume you're in an Ajax callback, different browsers go through different stages in doing an HTTP fetch. Chrome is aware of an intermediate state before "ready" and calls the callback in case you can do something useful with that fact; Firefox only finds out about the request when the data is ready. Nothing to do with JavaScript per se, just minor differences in the implementation of [XHR][1].


Correct code will be:

var check=httpReq.readyState==4?(httpReq.status==200?true:false):false;
if (check) {
   alert("Page done loading and is OK");
}


function check(){
        var check=httpReq.readyState==4&&httpReq.status==200?true:false;
        alert(check);
}

this.connect=function(frm){
        if (isFirefox() && firefoxVersion() >= 3) {
            httpReq.onload = check;
        } else {
            httpReq.onreadystatechange = check;
        }
       httpReq.open('GET',url(frm),false);
       httpReq.send(null);
}}


Ok, I'll explain this in a bit more detail.

After you issue request readyState goes through 5 phases:

  • 0) Uninitialized
  • 1) Connection opened
  • 2) Request sent
  • 3) Receiving response
  • 4) Data transfer is completed

After data transfer is completed status property is changed according to server response code. Typically for successful transmission the code is 200. If internal server error occurs, this code will change but you'll still receive data or at least what could be sent.

So in order to prevent your alert from firing a number of times you need to test for state and then for response code:

if (httpReq.readyState == 4) {  // data is received, now we play with it
    if (httpReq.status == 200) {
        // process data
        alert('Data received!');
    } else {
        // handle server-side error
    }
}
0

精彩评论

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