aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/xmlhttprequest/readystatechange_event/index.html
blob: feae1d44e87d7f7420f7b7d02b5d4855dcc50644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
title: XMLHttpRequest.onreadystatechange
slug: Web/API/XMLHttpRequest/readystatechange_event
translation_of: Web/API/XMLHttpRequest/onreadystatechange
original_slug: Web/API/XMLHttpRequest/onreadystatechange
---
<div>{{APIRef}}</div>

<p>An <a href="/docs/Web/API/EventHandler" title="A possible way to get notified of Events of a particular type (such as click) for a given object is to specify an event handler using:"><code>EventHandler</code></a> that is called whenever the <code>readyState</code> attribute changes. The callback is called from the user interface thread. The <strong><code>XMLHttpRequest.onreadystatechange</code></strong> property contains the event handler to be called when the {{event("readystatechange")}} event is fired, that is every time the {{domxref("XMLHttpRequest.readyState", "readyState")}} property of the {{domxref("XMLHttpRequest")}} changes.</p>

<div class="warning">
<p><strong>Warning:</strong> This should not be used with synchronous requests and must not be used from native code.</p>
</div>

<p>The <code>readystatechange</code> event will not be fired when an <code>XMLHttpRequest</code> request is cancelled with the <a href="/docs/Web/API/XMLHttpRequest/abort">abort()</a> method.</p>

<div class="note">
<p>UPDATE: it's firing in the latest version of browsers (Firefox 51.0.1, Opera 43.0.2442.991, Safari 10.0.3 (12602.4.8), Chrome 54.0.2840.71, Edge, IE11). Example <a href="https://jsfiddle.net/merksam/ve5oc0gn/">here</a> - just reaload page few times.</p>
</div>

<h2 id="Syntax" name="Syntax">語法</h2>

<pre class="syntaxbox"><em>XMLHttpRequest</em>.onreadystatechange = <em>callback</em>;</pre>

<h3 id="值"></h3>

<ul>
 <li><code><em>callback</em></code> is the function to be executed when the <code>readyState</code> changes.</li>
</ul>

<h2 id="Example" name="Example">範例</h2>

<pre class="brush: js">var xhr = new XMLHttpRequest(),
    method = "GET",
    url = "https://developer.mozilla.org/";

xhr.open(<em>method</em>, <em>url</em>, true);
xhr.onreadystatechange = function () {
        if(xhr.readyState === XMLHttpRequest.DONE &amp;&amp; xhr.status === 200) {
            console.log(xhr.responseText);
        }
    };
xhr.send();</pre>

<h2 id="規範">規範</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('XMLHttpRequest', '#handler-xhr-onreadystatechange')}}</td>
   <td>{{Spec2('XMLHttpRequest')}}</td>
   <td>WHATWG living standard</td>
  </tr>
 </tbody>
</table>

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>

{{Compat("api.XMLHttpRequest.readystatechange_event")}}