readyState== 3のOnReadyStateChangeイベントを検出するには、jQuery Prefiltersを使用できますか?

StackOverflow https://stackoverflow.com/questions/6035987

質問

jQueryでXHRストリーミングコメットテクニックを実装しようとしていますが、onreadystatechangeイベントの検出に問題があります。私はFirefox 4.0.1を使っています。

先頭(jQuery 1.5以降)を使用するXHRオブジェクト、具体的には、onreadystatechangeイベントにイベントハンドラを追加します。 http://jsfiddle.net/rdx6f/ には、その原理実装があります。そのコードは、readyStateイベントが発生したときにXHRオブジェクトのresponseTextおよびonreadystatechange属性を文書に追加します。ただし奇妙に、onreadystatechangeが1のときのreadyStateイベントのみを検出するようです。

換言すれば、jQueryは、XMLHTPRequestオブジェクトが状態 "Opened"(readyState 1)の状態であるが、XHRオブジェクトが状態「ヘッダ受信」(readyState 2)のときではない場合、jQueryはonreadystatechangeイベントを検出しているようです。 "(readyState 3)、" DONE "(ReadyState 4)。

奇妙に、イベントハンドラのalert()の場合、i do RemySyStateが1,2,3,4の場合は、 http://jsfiddle.net/rdx6f/1/ http://jsfiddle.net/rdx6f/ の場合、ドキュメントに追加する代わりにonreadystatechangeingだけです。< / P>

alert()がネイティブJavaScriptで1,2,3または4のときにonreadystatechangeイベントを検出することもできます - http:/ /jsfiddle.net/d7vah/ 。 (そのコードは、Dylan SchiemannのXHRストリーミングのXHRストリーミングの実装からほとんど逐語的に取られます。)

jQueryのAjax Prefiltersが間違っているか、またはJavaScriptの間違いをするのと同じように、ここで何か悪いことをしていますか?これはおそらくjQueryのバグですか?

役に立ちましたか?

解決

FYI: I looked into this quite deeply and it turns out that jQuery (since 1.5) uses the onreadystatechange event internally. The consequence of this is that jQuery will always write it's internal function to the onreadystatechange event, overwriting whatever you set it to. This happens whenever the AJAX call is actually sent, which means the function you wrote for onreadystatechange will work until that time (which means it will work until readyState = 1, but not longer).

This is the proposed workaround at the moment: http://jsfiddle.net/tBTW2/

At the moment, I cannot see a different (better) workaround without either not using jQuery (or going back to 1.4.4 or lower version, where it works perfectly) or editing the jQuery code (because as it is right now they will always overwrite whatever you try to hook in).

However, in upcoming versions (most likely in 1.8), the jQuery team are planning on implementing support for triggering events depending on the different readyStates. They will use Deferreds in some way.

This is the ticket that keeps this upcoming change:

http://bugs.jquery.com/ticket/9883

他のヒント

Here is the prefiltering done correctly linked from the jQuery bugzilla bug #8327,

http://jsfiddle.net/d8ckU/1/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top