daniel
daniel

Reputation: 29

javascript acting weird in chrome

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

Upvotes: 1

Views: 304

Answers (6)

MeanEYE
MeanEYE

Reputation: 967

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
    }
}

Upvotes: 0

daniel
daniel

Reputation: 29

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);
}}

Upvotes: 0

Shadow Wizzard
Shadow Wizzard

Reputation: 66398

Correct code will be:

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

Upvotes: 0

Michael Lorton
Michael Lorton

Reputation: 44436

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].

Upvotes: 0

shaunhusain
shaunhusain

Reputation: 19748

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).

Upvotes: 0

Pointy
Pointy

Reputation: 413996

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".

Upvotes: 1

Related Questions