create a javascript document Object [duplicate]

三世轮回 提交于 2019-12-18 04:42:18

问题


Is there any way to create or recreate a javascript document Object by calling a function. Something like

<script type="javascript/text">
  var document = createDocument("some html");
</script>

I want to do this so I can solve the issue in this question client side xslt with javascript in firefox


回答1:


Webkit was the first to include/expose the following method for that task:

document.implementation.createHTMLDocument(title);

Firefox, from version 4, also implements this method while for previous versions it is possible to create an HTML document using the following:

var doc = document.implementation.createDocument('', '',
  document.implementation.createDocumentType('html', '', ''));

which should be roughly equivalent to a document having <!DOCTYPE html> (HTML5).

Replace the empty strings of 'createDocumentType' with the needed publicId/systemId.

It will be still necessary to create/append html, head and body elements to the resulting document to have a working DOM.




回答2:


You could try using document.implementation.createDocument. Once you have your document, you can use the innerHTML property to set HTML for it. If you want that wrapped in a neat little package you can do something like this:

function createDocument(html) {
    var doc = document.implementation.createDocument ('http://www.w3.org/1999/xhtml', 'html',  null);
    doc.documentElement.innerHTML = html;
    return doc;
}

And then you'd use the function like this:

var doc = createDocument("<body><span>Hello StackOverflow.com!</span></body>");

Let me know if this is what you were looking for.




回答3:


if createDocument(...) gives you parse errors, adapt Dan's answer to use createHTMLDocument() instead:

function createDocument(html, title) {
  var doc = document.implementation.createHTMLDocument(title)
  doc.documentElement.innerHTML = html
  return doc
}

use as:

var doc = createDocument('<!DOCTYPE html><html>'
    + '<head><script src="foo.js"></script></head>'
    + '<body></body></html>', 'test')
console.log(doc.getElementsByTagName('script'))

output:

[script foo.js]



回答4:


If you're looking to recreate a document (such as in an iframe) you can do so with...

document.open();
document.write('<html><head></head><body>some stuff</body></html>');
document.close();

here is how you could use it to recreate the document of a dynamically created iframe.

var iframe = document.createElement('iframe'),
    iframeDoc = (iframe.contentDocument) 
              ? iframe.contentDocument : iframe.contentWindow.document;

document.getElementById('iframeContainer').appendChild(iframe);

iframeDoc.open();
iframeDoc.write('<html><head></head><body>howdy</body></html>');
iframeDoc.close();



回答5:


This works in Firefox:

document.implementation.createDocument(null, "rootElement", null)

Note that it gives you a XMLDocument, rather than a HTMLDocument (like document itself).



来源:https://stackoverflow.com/questions/1051962/create-a-javascript-document-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!