Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between JavaScript and DOM?

What is the difference between JavaScript and DOM? Is DOM related to Firefox? Is DOM just a source order of HTML elements?

like image 936
Jitendra Vyas Avatar asked Apr 28 '10 03:04

Jitendra Vyas


People also ask

What is the relation between DOM and JavaScript?

Simply put, JavaScript allows you to manipulate the DOM AKA Document object model that controls the client side scripting.

What does DOM mean in JavaScript?

Introduction. The Document Object Model (DOM) is an application programming interface (API) for HTML and XML documents. It defines the logical structure of documents and the way a document is accessed and manipulated.

Does JavaScript include DOM?

With the HTML DOM, JavaScript can access and change all the elements of an HTML document.

What is JavaScript and DOM scripting?

"DOM Scripting" means using JavaScript to manipulate the DOM (page elements and contents) inside a web page, either during page load, or in response to user events, like clicking a button or selecting text.


2 Answers

DOM stands for Document Object Model and, as you could guess from the name, represents the structure of an HTML/XML document in a platform/browser independent way. The DOM also provides an API to manipulate the DOM, with functions like getElementsByTagName and createElement.

JavaScript is a programming language that web browsers can execute. JavaScript can interact with the DOM with DOM scripting.

Edit to answer your question in a comment: For example, the browser downloads the HTML along with any referenced JS and CSS (and images, Flash etc.). The browser constructs the DOM from the HTML and renders it using the rules specified in the CSS. JS may manipulate the DOM when the page loads, when the user does something, or when any other event happens. When the DOM changes the browser updates what is displayed.

like image 163
David Johnstone Avatar answered Oct 11 '22 13:10

David Johnstone


As others have said, the DOM (Document Object Model) is essentially the API one uses to manipulate an HTML (or XML) document -- usually using JavaScript, since that's the language we have in the browser, but not always, as there are DOM-like APIs for manipulating these documents in other languages on the server side or the desktop, for example: http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/package-summary.html .

JavaScript is just a programming language. It happens to be the de facto standard scripting language for most (if not all) web browsers, and so in practice, most of the time when you're writing DOM manipulation scripts to be run on the client side, you're working with both the DOM and JavaScript at the same time.

However, It doesn't have to be like that. Someone could write a web browser (or a plugin for a web browser) that lets programmers write their DOM-manipulation scripts in Python, Ruby, C, Scheme, etc (in fact, JavaScript started life at Netscape as a Scheme).

Also, there are JavaScript interpreters (and even compilers) that run completely outside web browsers. In fact, if you want to get a feel for what the core JavaScript language is, you might try doing a little bit of scripting using Mozilla's Rhino: http://www.mozilla.org/rhino/ . There's no default DOM, no window object, nothing associated with a browser by default (though you can import some Java DOM packages).

I'd also recommend reading the old JavaScript 1.5 spec over at MDC (http://developer.mozilla.org/en/Core_JavaScript_1.5_Guide ) and some of their material on the DOM (http://developer.mozilla.org/en/DOM ).

like image 38
Weston C Avatar answered Oct 11 '22 13:10

Weston C