Javascript Basics & HTML Dom
Javascript Basics & HTML Dom
Sang Shin Java Technology Architect Sun Microsystems, Inc. sang.shin@sun.com www.javapassion.com
Topics
What is and Why JavaScript? How and Where do you place JavaScript code? JavaScript language JavaScript functions JavaScript events JavaScript objects JavaScript HTML DOM objects Closure (need to be added)
3
What is JavaScript?
Was designed to add interactivity to HTML pages Is a scripting language (a scripting language is a lightweight programming language) JavaScript code is usually embedded directly into HTML pages JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
JavaScript Language
JavaScript Variable
You create a variable with or without the var statement
var strname = some value strname = some value
When you declare a variable within a function, the variable can only be accessed within that function If you declare a variable outside a function, all the functions on your page can access it The lifetime of these variables starts when they are declared, and ends when the page is closed
12
Confirm box
> User will have to click either "OK" or "Cancel" to proceed > confirm("sometext")
Prompt box
> User will have to click either "OK" or "Cancel" to proceed after
13
JavaScript Language
Conditional statement
> if, if.. else, switch
Loop
> for loop, while loop
try...catch throw
14
JavaScript Functions (which behave like Java methods) More on Functions in other Presentation
JavaScript Funcitons
A JavaScript function contains some code that will be executed only by an event or by a call to that function
> To keep the browser from executing a script as soon as the
You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external .js file). Functions can be defined either <head> or <body> section
> As a convention, they are typically defined in the <head>
section
16
17
JavaScript Events
A mouse click A web page or an image loading Mousing over a hot spot on the web page Selecting an input box in an HTML form Submitting an HTML form A keystroke
19
Events
onabort - Loading of an image is interrupted onblur - An element loses focus onchange - The content of a field changes onclick - Mouse clicks an object ondblclick - Mouse double-clicks an object onerror - An error occurs when loading a document or an image onfocus - An element gets focus onkeydown - A keyboard key is pressed
20
Events
onkeypress - A keyboard key is pressed or held down onkeyup - A keyboard key is released onload - A page or an image is finished loading onmousedown - A mouse button is pressed onmousemove - The mouse is moved onmouseout - The mouse is moved off an element onmouseover - The mouse is moved over an element onmouseup - A mouse button is released
21
Events
onreset - The reset button is clicked onresize - A window or frame is resized onselect - Text is selected onsubmit - The submit button is clicked onunload - The user exits the page
22
24
25
onSubmit
The onSubmit event is used to validate all form fields before submitting it. Example: The checkForm() function will be called when the user clicks the submit button in the form. If the field values are not accepted, the submit should be canceled. The function checkForm() returns either true or false. If it returns true the form will be submitted, otherwise the submit will be cancelled:
<form method="post" action="xxx.html" onsubmit="return checkForm()">
26
27
JavaScript Objects
JavaScript Object
JavaScript is an Object Oriented Programming (OOP) language A JavaScript object has properties and methods
> Example: String JavaScript object has length property and
toUpperCase() method
31
Differences
> JavaScript object can be dynamically typed while Java object
is statically typed > In JavaScript, properties and methods are dynamically added
32
constructor for the Object class > Create a template (Constructor) first and then create an instance of an object from it > Create object instance as Hash Literal
34
Add properties to it
personObj.firstname="John"; personObj.age=50;
35
Note that the following two lines of code are doing completely different things
// Set property with a function personObj.tellYourage=tellYourage; // Set property with returned value of the function personObj.tellYourage=tellYourage();
36
39
41
class, a class that is capable of binding member fields and functions to itself at runtime
43
prototype
A prototype is a property of every JavaScript object Functions and properties can be associated with a constructor's property When a function is invoked with new keyword, all properties and methods of the prototype for the function are attached to the resulting object
45
prototype
// Constructor of the MyObject function MyObject(name, size){ this.name=name; this.size=size; } // Add a function to the prototype MyObject.prototype.tellSize=function{ alert(size of + this.name+ is + this.size); } // Create an instance of the object. The new object has tellSize() method. var myObj=new MyObject(Sang, 30 inches); myObj.tellSize(); 46
storing properties by name > Function objects can have other function objects as methods
48
49
HTML DOM
The HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate HTML documents All HTML elements, along with their containing text and attributes, can be accessed through the DOM.
> The contents can be modified or deleted, and new elements
can be created.
52
54
Document Object
56
57
58
59
60
61
Event Object
63
64
65
66
JavaScript Basics
Sang Shin Java Technology Architect Sun Microsystems, Inc. sang.shin@sun.com www.javapassion.com