Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

HTML in XMLHttpRequest

BaselineWidely available *

The W3CXMLHttpRequest specification addsHTML parsing support toXMLHttpRequest, which originally supported onlyXML parsing. This feature allows Web apps to obtain an HTML resource as a parsedDOM usingXMLHttpRequest.

To get an overview of how to useXMLHttpRequest in general, seeUsing XMLHttpRequest.

Limitations

To discourage the synchronous use ofXMLHttpRequest, HTML support is not available in the synchronous mode. Also, HTML support is only available if theresponseType property has been set to"document". This limitation avoids wasting time parsing HTML uselessly when legacy code usesXMLHttpRequest in the default mode to retrieveresponseText fortext/html resources. Also, this limitation avoids problems with legacy code that assumes thatresponseXML isnull for HTTP error pages (which often have atext/html response body).

Usage

Retrieving an HTML resource as a DOM usingXMLHttpRequest works just like retrieving an XML resource as a DOM usingXMLHttpRequest, except you can't use the synchronous mode and you have to explicitly request a document by assigning the string"document" to theresponseType property of theXMLHttpRequest object after callingopen() but before callingsend().

js
const xhr = new XMLHttpRequest();xhr.onload = () => {  console.log(xhr.responseXML.title);};xhr.open("GET", "file.html");xhr.responseType = "document";xhr.send();

Character encoding

If the character encoding is declared in the HTTPContent-Type header, that character encoding is used. Failing that, if there is a byte order mark, the encoding indicated by the byte order mark is used. Failing that, if there is a<meta> element that declares the encoding within the first 1024 bytes of the file, that encoding is used. Otherwise, the file is decoded as UTF-8.

Specifications

Specification
XMLHttpRequest
# interface-xmlhttprequest

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp