It was developed under the name Mocha, but was officially called LiveScript and renamed into JavaScript in 1995 by Brendan Eich at Netscape Communications as the scripting language for the Netscape Navigator Browser. It has been standardized by ECMA International as ECMAScript.

JavaScript + DOM/BOM + CSS + (X)HTML = DHTML.

JavaScript code in (X)HTML pages can be executed "on the fly" as the document is rendered (i.e. code outside of functions executes as the <script> element is encountered); in most cases though, JavaScript code is executed in response to document events (e.g. clicking a page element).

Versions

Frameworks

JavaScript can be linked or embedded in web pages using the <script> element.

Basic Language Features

The DOM (Document Object Model)

There are many methods and properties available for the DOM and to demonstrate let's look at it in action

Retrieving DOM Elements

Consider the follwing snippet:

HTML


            <div>
                <p id="first-paragraph" class="odd" name="first">first</p>
                <p class="even">second</p>
                <p class="odd">third</p>
                <p class="even">fourth</p>
            </div>
            <div>
                <p name="first">first second div</p>
            </div>
        

JavaScript


            // Get Element By specified id
            var firstParagraph = document.getElementById('first-paragraph');
            // Get "Elements" by Class Name
            // Note: that this returs the array of nodes
            var oddNodes = document.getElementsByClassName('odd');
            // oddNodes contains an array, retrieving the first element
            // can look something like this
            var firstOddNode = oddNodes[0]; // first odd node
            // Getting "Elements" By name. Also returns an array
            var firstParagraphs = document.getElementsByName('first-paragraphs'); // returns an array
            // Get by tag name. Also returns an array
            var allParagraphs = document.getElementsByTagName('p'); // all <p> tags will be returned as an array

            /* Using CSS Selectors */
            /* This is a powerful selector feature that we can use to select elements using their css selectors */
            /* to demonstrate: */

            // Instead of
            var allParagraphs = document.getElementsByTagName('p');
            // We can just do this:
            var allParagraphs = document.querySelectorAll('p');
            // For single values, such as an id
            var firstParagraph = document.querySelector('#first-paragraph');
        

Creating Nodes

The DOM has multiple methods to create instances of nodes. Below are the methods used for creating nodes:

createElement(tagName)
Used for creating tags
createTextNode(nodeValue)
This creates a text node which can then be used to add text inside an element node
cloneNode(deep: boolean)
This is for creating copies of nodes, the deep argument is boolean. If it's true it copies the entire tree of the node

To demonstrate we have the snippet below:


            // creates a p element
            var p = document.createElement('p');
            // creates a text node
            var text = document.createTextNode('Hello!');
            // appends the text node inside the created p element
            p.appendChild(text);
            document.body.appendChild(p);
        

This results with the ff:


            <body>
             <p>Hello!</p>
            </body>
        

Node Properties

HTML


            <p id="para">Lorem upsum...</p>
        

JavaScript


            var paragraph = document.getElementById('para'); // gets the paragraph
            // This changes the content 'Lorem ipsum...' to 'Hello'
            paragraph.textContent = "Hello";
            // Same as textContent above
            paragraph.innerText = "Hello";
            // If placing HTML use the innerHTML property
            paragraph.innerHTML = "Hello Chris";
            // This code replaces the whole tag.
            // After execution the element will become a div tag
            paragraph.outerHTML = "<div><p>Hello</p></div>"
        

We can also set attributes and styles using JavaScript


            var paragraph = document.getElementById('para');
            // Setting attributes
            paragraph.setAttribute('class', 'text-danger');
            // or better there is built-in a className property
            paragraph.className = "text-info";
            // Getting attributes
            paragraph.getAttribute('class'); // will return 'text-danger'
            // Setting the style via the style property
            paragraph.style = 'color: red'; // The text is now red
            // Setting the style via properties
            v.style.color = 'blue'; // Text now blue
            v.style.fontSize = '3em';
            v.style.backgroundColor = 'blue';
        

Adding, Removing and Reordering Nodes

appendChild(node)
Add the node to the last child node of where the node was used
inserBefore(node, reference)
Insert node before the reference
removeChild(childNode)
Removes specified node from its parent

Consider the following snippet


            var body = document.body;
            var p = document.createElement('p');
            p.innerText = "Hello";
            // appends created p element to body
            body.appendChild(p)

            var firstP = document.createElement('p');
            firstP.innerText = "First Paragraph";
            // inserts firstP before p
            p.parentNode.insertBefore(firstP, p);

            // Removes the second paragraph with the text 'Hello'
            p.parentNode.removeChild(p);

        

ECMAScript 5 Features

References