javascript - onreadystatechange() missing on XMLHttpRequest objects -


(function (send) {       xmlhttprequest.prototype.send = function () {           console.log(this.onreadystatechange); //null          send.apply(this, arguments);      };  })(xmlhttprequest.prototype.send); 

why this.onreadystatechange null? according this should exist , of type function.

onreadystatechange does exist...that's why it's null. if didn't exist, undefined. depends on whether it's been set already. it's property because it's null...if weren't, undefined...or @ least "onreadystatechange" in this false.

here's example:

var = new xmlhttprequest(); a.onreadystatechange = function () { }; a.send(); 

your console log function.

if had:

var b = new xmlhttprequest(); b.send(); 

your console log null.

here's demo of happening: http://jsfiddle.net/3xkx7/

(i left other steps out sending ajax request, explanation)

i'm not trying imply there's relation between null , undefined - i'm saying when creating new xmlhttprequest, onreadystatechange set null internally. whether set function or not you.

update:

the normal way creating/sending xhr order:

var xhr = new xmlhttprequest(); xhr.onreadystatechange = function () {    // , `open` flip-flopped     // whatever }; xhr.open("post", url, true); xhr.send("data=data"); 

i told/shown create new xmlhttprequest, whatever order of setting onreadystatechange , calling open, after both of should call send.

so, if done, onreadystatechange (if set) not null , function.

since mentioned using jquery, decided test overriding send method want, , making jquery $.ajax call (not sending xmlhttprequest manually). here's testing - http://jsfiddle.net/3xkx7/1/

it doesn't make sense why onreadystatechange null, because jquery has binding event in order know when request completes , state. decided @ jquery source code. , found order call things is:

xhr.open() xhr.send() xhr.onreadystatechange = function () {  }; 

which means when you're overriding send method, onreadystatechange null since jquery doesn't set before calling send. don't know reasoning it...it might prevent things you're doing...but never heard of or saw convention being used.

so reason you're getting value null because jquery sets onreadystatechange after calling send. meaning when override send , try access onreadystatechange property, isn't set yet.

hopefully example can understand: http://jsfiddle.net/dmp6q/16/


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -