This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

abort

Summary

Stops an asynchronous XMLHttpRequest in progress.

Method of apis/xhr/XMLHttpRequestapis/xhr/XMLHttpRequest

Syntax

 .abort();

Return Value

No return value

Examples

function request() {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://localhost/test.xml", true);
    xhr.send();
    return xhr;
}

document.getElementById('submit').addEventListener('click', function () {
    var xhr = request(),
        submit = this,
        cancel = document.getElementById('cancel');

    function detach() {
        submit.removeEventListener('click', canceling, false);
        cancel.removeEventListener('click', canceling, false);
    }

    function canceling() {
        detach();
        xhr.abort();
    }

    // detach any remaining handlers after XHR finishes
    xhr.addEventListener('load', detach, false);

    // cancel if "Submit" is clicked again before XHR finishes
    submit.addEventListener('click', canceling, false);

    // cancel if "Cancel" is clicked
    cancel.addEventListener('click', canceling, false);
}, false);

Notes

Calling abort resets the object; the readyState is changed to 0 (uninitialized). Calling it on an already aborted request throws an exception.

Related specifications

W3C XMLHttpRequest Specification
W3C Working Draft

Attributions