简体   繁体   中英

How do I request an HTML document in Javascript?

I know I can send a request for an XML document with XMLHttpRequest, which will also parse the received XML document. Unfortunately that's not going to work with HTML all the time. Unlike XML, HTML has some tags that don't require closing (such as the < img > tag). XML ALWAYS closes a tag, either as self closing like this < mytag / > or as a pair of tags like this < mytag > < / mytag >. XMLHttpRequest attempts to parse the received XML, but if it's HTML with unclosed tags, it's going to fail. Is there a better way of having Javascript download and parse an HTML document, which contains unclosed tags?

I know I can send a request for an XML document with XMLHttpRequest , which will also parse the received XML document.

Yes , but your understanding is incomplete. XMLHttpRequest can be used not only for XML documents, but any HTTP request (with limitations only on cross-origin activity, not content).

Unfortunately that's not going to work with HTML all the time. Unlike XML, HTML has some tags that don't require closing (such as the <img> tag). XML ALWAYS closes a tag, either as self closing like this <mytag /> or as a pair of tags like this <mytag></mytag> .

You are correct insofar as HTML is not XML (indeed: HTML was an application of SGML, as was XML, but since HTML5 it's it's own thing ), and HTML cannot be reliably parsed as XML. However you are completely incorrect about XMLHttpRequest being unable to handle HTML responses: in a web-page JavaScript environment you can load the HTML responses into the main document DOM, or a separate document fragment and let the browser parse the HTML5 tag-soup just like a normal web-page request.

XMLHttpRequest attempts to parse the received XML, but if it's HTML with unclosed tags, it's going to fail.

You are mistaken: XMLHttpRequest will only attempt to parse the response as XML if you tell it to . If you tell XMLHttpRequest to give you the response as text/ string or a JSON blob you can do that too.

Is there a better way of having Javascript download and parse an HTML document, which contains unclosed tags?

Yes: you set responseType = 'document' - that instructs XMLHttpRequest to expect and handle a HTML document response, which will be exposed through the (confusingly named!) responseXML property.

However , it's 2021 now - you should not be using XMLHttpRequest - you should be using fetch instead: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM