1

Is there any way to create a javascript document Object by calling a function? Something like this:

<script type="javascript/text">
  var myDoc = createDocument("example.html");
</script>

I want to be able to create and manipulate a DOM from a string, 'example.com'.

6
  • is "example.html" the local file? Commented Mar 3, 2017 at 21:31
  • "example.html" is in a variable within the javascript program. Commented Mar 3, 2017 at 21:33
  • Could you be more specific in your question? Commented Mar 3, 2017 at 21:38
  • Is example.html an html document? Commented Mar 3, 2017 at 21:39
  • Yes it is an html document. Commented Mar 3, 2017 at 21:44

1 Answer 1

0

Initially you need to get html through AJAX request after use DOMParser.

var parser = new DOMParser();
var doc = parser.parseFromString("<html_string>", "text/html");

OR

var doctype = document.implementation.createDocumentType(
    'html',
    '-//W3C//DTD XHTML 1.0 Strict//EN',
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
);

var dom = document.implementation.createDocument(
    'http://www.w3.org/1999/xhtml',
    'html',
    doctype
);

 // assign the body of your page, after you can use dom variable.
dom.documentElement.innerHTML = '<head></head><body></body>';
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.