Chapter06 JavaScript
Chapter06 JavaScript
Chapter06 JavaScript
Scripting
Chapter 6
.HTML
.CSS
.JavaScript
WHAT IS JAVASCRIPT
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
What is JavaScript
• Inline
• Embedded
• External
SYNTAX
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
JavaScript Syntax
(x===9) is true
(x!==9) is false
The Boolean operators and, or, and not and their truth
tables are listed in Table 6.2. Syntactically they are
represented with && (and), || (or), and ! (not).
JAVASCRIPT OBJECTS
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
JavaScript Objects
Objects not Classes
Arrays are one of the most used data structures. In practice, this
class is defined to behave more like a linked list in that it can be
resized dynamically, but the implementation is browser specific,
meaning the efficiency of insert and delete operations is
unknown.
The following code creates a new, empty array named greetings:
The String class has already been used without us even knowing
it.
Constructor usage
var greet = new String("Good"); // long form constructor
var greet = "Good"; // shortcut constructor
Length of a string
alert (greet.length); // will display "4"
The Date class is yet another helpful included object you should be
aware of. It allows you to quickly calculate the current date or
create date objects for particular dates. To display today’s date as a
string, we would simply create a new object and use the toString()
method.
var d = new Date();
// This outputs Today is Mon Nov 12 2012 15:40:19 GMT-0700
alert ("Today is "+ d.toString());
Property Description
attributes Collection of node attributes
Method Description
Creates an attribute node
createAttribute()
Property Description
className The current value for the class attribute of this HTML element.
JAVASCRIPT EVENTS
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
JavaScript Events
function someHandler(e) {
// e is the event that triggered this handler.
}
• mouse events
• keyboard events
• form events
• frame events
Event Description
onclick The mouse was clicked on an element
ondblclick The mouse was double clicked on an element
onmousedown The mouse was pressed down over an element
onmouseup The mouse was released over an element
onmouseover The mouse was moved (not clicked) over an element
onmouseout The mouse was moved off of an element
onmousemove The mouse was moved while over an element
Event Description
onkeyup The user releases a key that was down (this happens last)
onchange Some <input>, <textarea> or <select> field had their value change.
This could mean the user typed something, or selected a new choice.
onreset HTML forms have the ability to be reset. This event is triggered when
that happens.
onselect When the users selects some text. This is often used to try and
prevent copy/paste.
onsubmit When the form is submitted this event is triggered. We can do some
pre-validation when the user submits the form in JavaScript before
sending the data on to the server.
Event Description
FORMS
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
Validating Forms