Javascript
Javascript
Javascript
JavaScript
Introduction
What is JavaScript?
Is client side scripting language.
JavaScript was designed to add interactivity to
HTML pages.
JavaScript is introduced into HTML using
<script> tag in head or body section or
imported form external JS file.
Everyone can use JavaScript without purchasing
a license.
What can a JavaScript do?
JavaScript gives HTML designers a programming tool .
do
{
code to be executed
}
while (var<=endvalue);
Break and Continue
• The break statement will break the loop and
continue executing the code that follows after
the loop (if any).
• Special Characters
– \‘, \“, \\, \n, \t
Array
• The Array object is used to store multiple values in a
single variable.
– You Can Have Different Objects in One Array.
• Creating an Array
– Using an array literal
• var array-name = [item1, item2, ...];
– Using the JavaScript Keyword new
• var cars = new Array("Saab", "Volvo", "BMW");
• Accessing the elements
– You refer to an array element by referring to the index
number.
Cont.…..
• Array Properties and Methods
– Length property
• Looping
– For, while
• Methods
– join() method
– It behaves just like toString(), but you can specify the separator.
– fruits.join(" * ")
– Popping and Pushing
– fruits.pop(), fruits.push("Kiwi")
Cont.….
– The shift() method removes the first element of an
array, The unshift() method adds a new element to
an array (at the beginning).
– fruits.shift(), fruits.unshift("Lemon")
– Deleting Elements
– delete fruits[0];
– The sort() method sorts an array alphabetically
– fruits.sort()
– The reverse() method reverses the elements in an
array.
– fruits.reverse();
Cont.……
– The concat() method creates a new array by
concatenating two arrays:
• var myGirls = ["Cecilie", "Lone"];
var myBoys = ["Emil", "Tobias","Linus"];
var myChildren = myGirls.concat(myBoys);
• Reading Assignment On Date.
Dynamic HTML (DHTML)
• DHTML is the art of combining HTML, JavaScript, DOM, and
CSS.
• DHTML is NOT a language or a web standard.
• Dynamic style
– Because the Dynamic HTML (DHTML) Document Object Model (DOM)
makes every HTML element and attribute accessible, it is easy to use
scripts to dynamically read and change styles.
– When the name of the style is more than one word use camel casing
convention.
Cont.……
• Dynamic Content
– With DHTML, you can change the content of the page after it is
loaded.
– It also enables creating, inserting, and deleting elements.
– Changing attribute values.
– Standard DOM Methods
• createElement - Creates a new element (node) of the specified type.
• createTextNode - Creates a plain text node (no HTML).
• appendChild - Adds the node to the parent element as the last child.
• insertBefore - Inserts the node into the document as a child node of the parent.
– var newItem = document.createElement("LI") // Create a <li> node
var textnode = document.createTextNode("Water") // Create a text node
newItem.appendChild(textnode) // Append the text to <li>
var list = document.getElementById("myList") // Get the <ul> element to insert a new node
list.insertBefore(newItem, list.childNodes[0]);
• replaceChild - Replaces an existing child element with a new child element.
Cont.…..
• Timing Events
– With JavaScript, it is possible to execute some code at
specified time-intervals. This is called timing events
– The two key methods that are used are:
• setInterval() - executes a function, over and over again, at
specified time intervals
– myVar = window.setInterval("javascript function", milliseconds);
– There are 1000 milliseconds in one second
– window.clearInterval(intervalVariable)
• setTimeout() - executes a function, once, after waiting a
specified number of milliseconds
– myVar = window.setTimeout("javascript function", milliseconds);