In javascript, I can do:
img1 = new Image();
img2 = document.createElement('img');
my question is, is there a difference between the two approach? I’ve read somewhere that Image, Form, and Element is called host objects, is this true? If it is, what are host objects?
Which approach is preferable?
I couldn’t find any detailed reference but based on the comment in the MDC –
HTMLImageElementexample, it seems thatImageis part of DOM level 0 whereasdocument.createElementis part of DOM level 2.DOM level 0 was invented by Netscape and provided a way to access the certain elements of the website. Basically all browsers support it for backwards compatibility.
But to be honest, I don’t understand why the
Imageconstructor exists there, because, as far as I understood it, there was no way to manipulate the document with DOM level 0. Maybe it was only used internally by the browser to create the objects.DOM level 2 is an official standard developed by the W3C.
For more information about the DOM levels, have a look at at quirksmode.org – Level 0 DOM and Wikipedia.
Yes.
The ECMAScript specification motivates host objects this way:
and
So any object that is not defined in the specification and provided by the environment is a host object. These are for example in a browser (among others):
window,documentandconsole.