A first look to the DOM model

Questo articolo l'ho scritto in inglese, me ne sono accorto quando ero arrivato circa a metà... non chiedetemi il perchè, perdonatemi i probabili errori di grammatica, ma non avevo voglia di riscrivere in italiano tutto quanto.



DOM Introduction

A first look to the DOM model

DOM means Document Object Model. It's a model of how XML and HTML are represented in a tree structure.
DOM is a platform neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. The document can be further processed and the results of that processing can be incorporated back into the presented page.

DOM provides an OO API that allows parsing XML and HTML into a well defined tree structure and operating on its contents.

DOM basic manipulation

Destroying and creating objects

The first basic example is to show the basic methods for get an element and remove it from the DOM-tree. The code is very simple:


<script language="javascript" type="text/javascript">
function destroyObject(obj)
{
	var formElement = document.getElementById(obj);
	var parentElement = formElement.parentNode;

	parentElement.removeChild(formElement);
};
</script>
	

The destroyObject() function get from the DOM-tree an element with the getElementById() method of the document class. DOM provide us a specific method for removing a subtree from the DOM-tree, the method is removeChild(). We get the parent node of the one we have to remove with the parentNode propriety of the element we have just catch with the getElementById() method.

This is an live example: clicking on the "Destroy" button you'll destroy the gray box.

The second example is a bit more difficult, it will recreate the box. How? We have to create the subtree and then we'll append it where we need.


<script language="javascript" type="text/javascript">
function createObject(obj) {
	var myDiv = document.createElement("div");
	var parentNode = document.getElementById(obj);
	myDiv.setAttribute("id","lorem");
	myDiv.appendChild(document.createTextNode("text"));
	parentNode.appendChild(myDiv);
};
</script>

The createElement() method create the new node, we can set an Attribute with the setAttribute() method.
To the new div node we can append a child node with the text (createTextNode()). For appending the new subtree we use the appendChild() method for link it where we need in the DOM-tree.

Here you can see the example in action.

Soon I'll try to study more of this topic, with few code you can get real dynamic web pages.

Ma l'hai passato poi l'esame d'inglese al CLA?

Mi sta che cmq ragionavi in puro italiano...

- stand for: "sta per", hai provato con "means" cioé "significa"

provo a correggertelo oggi e te lo invio per email :D

Certo che ho passato l'esame di inglese, ed anche bene direi. Il fatto è che dopo le undici di sera i criceti che girano nel mio cervello vanno a nanna, così non ragiono più bene. Aspetto la correzione comunque, denghiu!

"Perché", io mi chiedo... L'italiano è tanto bello... Perché bisogna scrivere in inglese? I tuoi criceti hanno fatto bene ad andare a nanna... Io, se fossi stata in loro, sarei entrata in sciopero! :P

Bhe la nostra sarà la lingua di Dante, ma l'inglese è pur sempre la lingua di Shakespeare. L'inglese è brutto a sentirlo parlare, ma rende benissimo nelle canzoni (sarà mica un caso che quasi tutte le canzoni di gran successo son proprio cantate in inglese e non in francese?) e nei poemi - tipo Pardise Lost e appunto Shakespeare.
In realtà, conserva una vaga parvenza di quella laconicità che aveva il latino e che invece l'italiano ha perduto, "manierizzandosi".
Nessuno che non è un native speaker puo scrivere in un inglese corretto, comunque quello di questa pagina va bene, e più che pensare alla differenza fra "stand for" e "means" (che sarebbero abbastanza intercambiabili) penserei alle terze persone singolari, spesso trascurate. Un errore tipico in inglese, e pensare che l'inglese ha probabilmente le piu facili declinazioni verbali del mondo...

In realtà se si abbandonano le tendenze accademiche, l'inglese più affascinante di tutti è proprio quello scritto dai madrelingua latini perchè ne tirano fuori delle sonorità e dei circuiti che, se sconcertano i madrelingua inglesi, sono invece assai affascinanti.
Non bisogna confondere l'insolito con lo sbagliato. E' un grave errore, ed è assai diffuso; non solo in linguistica ma anche in campo etico o, quantomeno, deontologico.