mercredi 22 août 2012

Basic JavaScript - Web programming

This post corresponds to notes taken while reading Eloquent JavaScript.

The open method of the window object takes an URL argument that will open on a new window. An opened window can be closed with its close method. To escape unwanted characters in the URL (e.g. space), the encodeURIComponent method can be used. To remove them again use decodeURIComponent method.
Every window object has a document property that contains the document shown in that window. To force the browser to load another document, someone can set the document.location.href property.
Example:
var perry = window.open("http://www.pbfcomics.com");
// show some information about the URL of the document
console.log(document.location.href);
perry.close();
// encoding URL
var encoded = encodeURIComponent("aztec empire");
console.log(encoded);
The document object has a property named forms which contains links to all the forms in the document. Example, if a form has a property name="userinfo" then it can be accessed as a propery named userinfo.
The object for the form tag has a property elements that refers to an object containing the fields of the form by their name.
Example:
var userForm = document.forms.userinfo;
console.log(userForm.method);
console.log(userForm.action);
// set the content (via the value property) of a text input element called 'name'
var nameField = userForm.elements.name;
nameField.value = "Eugène";


Aucun commentaire:

Enregistrer un commentaire