2

Create custom document object:

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

Try to write in it:

//Throws error - TypeError: doc.write is not a function
doc.write(document.documentElement.innerHTML);

Same goes for .close and .open. What the hell? Is this a bug? Probably not - all browsers do it:

Chrome (most helful error ever, Chrome must be fun to debug in):

Uncaught TypeError: undefined is not a function

Opera:

TypeError: Object #<Document> has no method 'write'

Firefox:

TypeError: document.implementation.createDocument(...).write is not a function

Why are the methods open, write and close missing for custom documents?

4
  • 3
    My guess would be because .write, .open and .close are horrible methods and should not be used? Commented Dec 11, 2014 at 13:21
  • What are you actually trying to do with that doc? Commented Dec 11, 2014 at 13:32
  • @NiettheDarkAbsol Why are they horrible? Do you propose better solution to the whole problem? Commented Dec 11, 2014 at 14:20
  • @Cerbrus I'm loading HTML dynamically, then copying certain node values to the loaded window.document. I'm trying to load next and previous comics using AJAX and preload them so that I don't have to wait... :) Commented Dec 11, 2014 at 14:25

1 Answer 1

3

document.implementation.createDocument returns a XMLDocument.
However, .write is a function document inherits from HTMLDocument.

Basically, XMLDocuments don't have that function.
You're probably looking for createHTMLDocument

Sign up to request clarification or add additional context in comments.

4 Comments

I'd really like to create HTMLDocument then.
Have a look at createHTMLDocument
According to MDN, XMLDocument inherits from Document which has the .write and other.
@TomášZato: "It inherits from the generic Document and does not add any specific methods or properties to it" "This interface doesn't inherit any method."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.