Java Script Basics
Java Script Basics
Scripting
Chapter 6
3 Using
JavaScript 4 Syntax
5 JavaScript
Objects 6 The DOM
7 JavaScript
Events 8 Forms
WHAT IS JAVASCRIPT
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
What is JavaScript
This type of AJAX development can be difficult but thankfully, the other
key development in the history of JavaScript has made AJAX
programming significantly less tricky. This development has been the
creation of JavaScript frameworks, such as jQuery, Prototype, ASP.NET
AJAX, and MooTools. These JavaScript frameworks reduce the amount
of JavaScript code required to perform typical AJAX tasks. Some of these
extend the JavaScript language; others provide functions and objects to
simplify the creation of complex user interfaces.
With this strategy you develop your site for the abilities of
current browsers.
For those users who are not using current browsers, you
might provide an alternate site or pages for those using
older browsers that lack the JavaScript (or CSS or HTML5)
used on the main site.
The idea here is that the site is “degraded” (i.e., loses
capability) “gracefully” (i.e., without pop-up JavaScript error
codes or without condescending messages telling users to
upgrade their browsers)
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
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"
Property Description
attributes Collection of node attributes
Method Description
createAttribute() Creates an attribute node
Property Description
className The current value for the class attribute of
this HTML element.
id The current value for the id of this
element.
innerHTML Represents all the things inside of the tags.
This can be read or written to and is the
primary way which we update particular
div's using JS.
style The style attribute of an element. We can
read and modify this property.
tagName The tag name for the element.
<div id="latestComment">
</div>
JAVASCRIPT EVENTS
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
JavaScript Events
function someHandler(e) {
// e is the event that triggered this handler.
}
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
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
user form input should be validated on both the client side
and the server side.
3 Using
JavaScript 4 Syntax
5 JavaScript
Objects 6 The DOM
7 JavaScript
Events 8 Forms