Basically that's the question, how is one supposed to construct a Document object from a string of HTML dynamically in javascript?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
The following works in most common browsers, but not some. This is how simple it should be (but isn't):
To account for user agent vagaries, the following may be better (please note attribution):
Don't be put off by the amount of code, there are a lot of comments, it can be shortened quite a bit but becomes less readable.
Oh, and if the markup is valid XML, life is much simpler:
An updated answer for 2014, as the DOMparser has evolved. This works in all current browsers I can find, and should work too in earlier versions of IE, using ecManaut's document.implementation.createHTMLDocument('') approach above.
Essentially, IE, Opera, Firefox can all parse as "text/html". Safari parses as "text/xml".
Beware of intolerant XML parsing, though. The Safari parse will break down at non-breaking spaces and other HTML characters (French/German accents) designated with ampersands. Rather than handle each character separately, the code below replaces all ampersands with meaningless character string "j!J!". This string can subsequently be re-rendered as an ampersand when displaying the results in a browser (simpler, I have found, than trying to handle ampersands in "false" XML parsing).
There are two methods defined in specifications,
createDocument
from DOM Core Level 2 andcreateHTMLDocument
from HTML5. The former creates an XML document (including XHTML), the latter creates a HTML document. Both reside, as functions, on theDOMImplementation
interface.In reality, these methods are rather young and only implemented in recent browser releases. According to http://quirksmode.org and MDN, the following browsers support
createHTMLDocument
:Interestingly enough, you can (kind of) create a HTML document in older versions of Internet Explorer, using
ActiveXObject
:The resulting object will be a new document, which can be manipulated just like any other document.
Per the spec (doc), one may use the
createHTMLDocument
method ofDOMImplementation
, accessible viadocument.implementation
as follows:DOMImplementation
: https://developer.mozilla.org/en/DOM/document.implementationDOMImplementation.createHTMLDocument
: https://developer.mozilla.org/En/DOM/DOMImplementation.createHTMLDocumentAssuming you are trying to create a fully parsed Document object from a string of markup and a content-type you also happen to know (maybe because you got the html from an xmlhttprequest, and thus got the content-type in its
Content-Type
http header; probably usuallytext/html
) – it should be this easy:in an ideal future world where browser
DOMParser
implementations are as strong and competent as their document rendering is – maybe that's a good pipe dream requirement for futureHTML6
standards efforts. It turns out no current browsers do, though.You probably have the easier (but still messy) problem of having a string of html you want to get a fully parsed
Document
object for. Here is another take on how to do that, which also ought to work in all browsers – first you make a HTMLDocument
object:and then populate it with your html fragment:
Now you should have a fully parsed DOM in doc, which you can run
alert(doc.title)
on, slice with css selectors likedoc.querySelectorAll('p')
or ditto XPath usingdoc.evaluate
.This actually works in modern WebKit browsers like Chrome and Safari (I just tested in Chrome 22 and Safari 6 respectively) – here is an example that takes the current page's source code, recreates it in a new document variable
src
, reads out its title, overwrites it with a html quoted version of the same source code and shows the result in an iframe: http://codepen.io/johan/full/KLIeESadly, I don't think any other contemporary browsers have quite as solid implementations yet.