That is, why do we not follow the standard JavaScript convention of using
var el = new Element("div");
but use
var el = document.createElement("div");
to do it?
(P.S. document
is an object of the class Document
. Element
is also a class, and both Document
and Element
class are defined in the browser environment).
All of the document.xxxx
methods are part of the DOM, not part of the Javascript language. It's a separate API that belongs to the browser, but which Javascript in the browser is allowed to access.
If another language were to be implemented in the browser, it would use the exact same API to access the DOM. In fact, VBScript in IE does exactly this - for proof, see some example code here. (but note, I'm not recommending actually using VBScript in a browser! Stick with JS)
And Javascript can be used outside of a browser environment (eg node.js), in which case it may not have or need a DOM class structure.
The DOM may also be implemented outside of a browser, and the same API would be available to any languages that use use it. For example, PHP has a DOMDocument class, which implements all the same DOM methods to add/remove/etc elements from the tree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With