Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Document objects with JavaScript

Basically that's the question, how is one supposed to construct a Document object from a string of HTML dynamically in javascript?

like image 624
jayarjo Avatar asked Nov 22 '11 13:11

jayarjo


People also ask

How do we get document object in JavaScript?

Document object − Each HTML document that gets loaded into a window becomes a document object. The document contains the contents of the page. Form object − Everything enclosed in the <form>...

What is the use of document object in JavaScript?

The document object represents your web page. If you want to access any element in an HTML page, you always start with accessing the document object.

What is a DOM in JavaScript?

What is the DOM? The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects; that way, programming languages can interact with the page.


2 Answers

There are two methods defined in specifications, createDocument from DOM Core Level 2 and createHTMLDocument from HTML5. The former creates an XML document (including XHTML), the latter creates a HTML document. Both reside, as functions, on the DOMImplementation interface.

var impl    = document.implementation,     xmlDoc  = impl.createDocument(namespaceURI, qualifiedNameStr, documentType),     htmlDoc = impl.createHTMLDocument(title); 

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:

  • Chrome 4
  • Opera 10
  • Firefox 4
  • Internet Explorer 9
  • Safari 4

Interestingly enough, you can (kind of) create a HTML document in older versions of Internet Explorer, using ActiveXObject:

var htmlDoc = new ActiveXObject("htmlfile"); 

The resulting object will be a new document, which can be manipulated just like any other document.

like image 184
Andy E Avatar answered Oct 14 '22 00:10

Andy E


Assuming 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 usually text/html) – it should be this easy:

var doc = (new DOMParser).parseFromString(markup, mime_type); 

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 future HTML6 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 HTML Document object:

var doc = document.implementation.createHTMLDocument(''); 

and then populate it with your html fragment:

doc.open(); doc.write(html); doc.close(); 

Now you should have a fully parsed DOM in doc, which you can run alert(doc.title) on, slice with css selectors like doc.querySelectorAll('p') or ditto XPath using doc.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/KLIeE

Sadly, I don't think any other contemporary browsers have quite as solid implementations yet.

like image 42
ecmanaut Avatar answered Oct 14 '22 00:10

ecmanaut