Professional Documents
Culture Documents
Web Prelims
Web Prelims
Lecture 1
INTRODUCTION TO
JAVASCRIPT
What is JavaScript?
• a dynamic computer programming language
• lightweight and most commonly used as a part of
web pages, whose implementations allow client-side
script to interact with the user and make dynamic
pages
• an interpreted programming language with object-
oriented capabilities
• The programs in this language are called scripts.
• can be written right in a web page’s HTML and run
automatically as the page loads
• Scripts are provided and executed as plain text. They
don’t need special preparation or compilation to run.
What is JavaScript? p2
• JavaScript is very different from another language called
Java.
• JavaScript initially had another name: “LiveScript”. But
Java was very popular at that time, so it was decided that
positioning a new language as a “younger brother” of Java
would help.
• As JavaScript evolved, it became a fully independent
language with its own specification called ECMAScript, and
now it has no relation to Java at all.
• JavaScript can execute not only in the browser, but also on
the server, or actually on any device that has a special
program called the JavaScript engine.
• The browser has an embedded engine sometimes called a
“JavaScript virtual machine”.
JavaScript History
• JavaScript, originally called LiveScript, was created by
Brendan Eich at Netscape in 1995.
• Soon after, Microsoft launched its own version of
JavaScript called JScript.
• Subsequently, Netscape submitted it to ECMA (formerly
"European Computer Manufacturers Association", now
"Ecma International - European association for
standardizing information and communication systems")
for standardization, together with Microsoft's JScript.
Versions
The ECMA Specification is called "ECMA-262 ECMAScript
Language Specification" (also approved as "ISO/IEC
16262"):
• ECMA-262 version 1 (June 1997)
• ECMA-262 version 2 (August 1998)
• ECMA-262 version 3 (December 1999)
• ECMA-262 version 4 - abandon due to political
differences
• ECMA-262 version 5 and 5.1 (June 2011) (@
http://www.ecma-
international.org/publications/standards/Ecma-262.htm)
• ECMA-262 version 6: June 2015.
What Is ECMAScript?
• ECMAScript (European Computer Manufacturers Association
Script) is sometimes abbreviated as "ES“.
• is a scripting language specification maintained and trademarked
by Ecma International
• ECMA developed the official standard for ECMAScript, often
known as ECMA-262 which helps in defining the ECMAScript
language and also ensures the consistency between web script
implementations.
• JavaScript, JScript and ActionScript are implementation of
ECMAScript.
• ECMAScript, along with Document Object Model, works similarly
to the current implementations of JScript and JavaScript.
NOTE: ECMAScript evolves over time which means new features
are added and new syntax is adopted. Not all browsers will
implement all newer features of ECMAScript in the same manner.
The ECMA-262 Specification defined a standard version of the core JavaScript language:
A Simplified Version
Of The HTML DOM.
The HTML DOM p2
• With the object model, JavaScript gets all the power it
needs to create dynamic HTML:
• JavaScript can change all the HTML elements in the page
• JavaScript can change all the HTML attributes in the page
• JavaScript can change all the CSS styles in the page
• JavaScript can remove existing HTML elements and
attributes
• JavaScript can add new HTML elements and attributes
• JavaScript can react to all existing HTML events in the
page
• JavaScript can create new HTML events in the page
The HTML DOM p3
HTML DOM defines:
• The HTML elements as objects
• The properties of all HTML elements
• The methods to access all HTML elements
• The events for all HTML elements
Comments Syntax
// This is a single-line comment.
/*
This is a
multi-line
comment.
*/
Accessing Elements
• Dot Notation - elements (and other objects) can be
referenced using dot notation, starting with the highest-
level object (i.e., window). Objects can be referred to by
name or id or by their position on the page. For example,
if there is a form on the page named "loginform", using dot
notation you could refer to the form as follows:
Syntax
window.document.loginform
Assuming that loginform is the first form on the page, you
could also refer to it in this way:
Syntax
window.document.forms[0]
Accessing Elements p2
• A document can have multiple form elements as children.
• The number in the square brackets ([]) indicates the
specific form in question.
• Every document object contains a collection of forms.
• The length of the collection could be zero (meaning there
are no forms on the page) or greater.
• In JavaScript, collections (and arrays) are zero-based,
meaning that the first form on the page is referenced with
the number zero (0) as shown in the syntax example on
the previous slide.
Accessing Elements p3
• Square Bracket Notation
• Objects can also be referenced using square bracket
notation as shown below:
Syntax
window['document']['loginform']
// and
window['document']['forms'][0]
• Dot notation and square bracket notation are completely
interchangeable.
• Dot notation is much more common; however, there are
times when it is more convenient to use square bracket
notation.
Where Is JavaScript Code Written?
• JavaScript code can be written inline (e.g., within HTML
attributes called on-event handlers), in script blocks, and in
external JavaScript files.
Code Sample javascript.html:
<!DOCTYPE html>
<html >
<head>
<script>
//Pop up an alert
window.alert("The page is loading");
</script>
<title>JavaScript Page</title>
</head>
Where Is JavaScript Code Written? p2
<body>
<main>
<button onclick="document.body.style.backgroundColor = 'red';">
Red
</button>
<button onclick="document.body.style.backgroundColor = 'white';">
White
</button>
<button onclick="document.body.style.backgroundColor = 'green';">
Green
</button>
<button onclick="document.body.style.backgroundColor = 'blue';">
Blue
</button>
<script src="script-2.js"></script>
</main>
</body>
</html>
Where Is JavaScript Code Written? p3
• Code Sample script.js :
• /*
• This script simply outputs
• "Hello, there!" to the browser.
• */
• document.write("<p>Hello, there!</p>");
The Implicit window Object
• The window object is always the implicit top-level object
and therefore does not have to be included in references
to objects.
• For example, window.document.write() can be shortened
to document.write().
• Likewise, window.alert() can be shortened to just alert().
JavaScript Objects, Methods and Properties
• JavaScript is used to manipulate or get information about
objects in the HTML DOM. Objects in an HTML page have
methods (actions, such as opening a new window or
submitting a form) and properties (attributes or qualities,
such as color and size).
• To illustrate objects, methods and properties, let's return
to the code in javascript.html and script.js.
Methods
• Methods are the verbs of JavaScript. They cause things to
happen.
window.alert() - HTML pages are read and processed from
top to bottom. Because window is the implicit top-level
object, we could leave it off and just write alert("The page is
loading"). When the browser reads that line of code, it will
pop up an alert box and will not continue processing the
page until the user presses the OK button. Once the user
presses the button, the alert box disappears and the rest of
the page loads.
document.write() - The write() method of the document
object is used to write out code to the page as it loads. It is
often used to write out dynamic data, such as the date and
time on the user's machine.
Arguments in Methods
• Methods can take zero or more arguments separated by
commas.
Syntax
object.method(argument1, argument2);
document.body.style.backgroundColor
• The body object is a property of the document object
• The style object is a property of the body object
• The backgroundColor is a read-write property of the style object.
• Looking back at javascript.html, the four button elements use the
onclick on-event handler to catch click events. When the user
clicks on a button, JavaScript is used to set the background of
the body to a new color, in the same way that we might use CSS
to style the page with background-color:red or background-
color:white.
Activity 1
Alerts, Writing, & Changing Background Color
• Duration: 5 to 15 minutes.
1. In this exercise, you will practice using JavaScript to pop up
an alert, write text to the screen, and set the background
color of the page.
2. Create an HTML document named activity1.html
3. In the head of the file, add a JavaScript alert which pops up
the message "Welcome to my page!" when the page loads.
4. Add click handlers to the two buttons to allow the user to
change the background color of the page to red or to blue.
5. In the script at the bottom of the page, use JavaScript to
write the text "This text was generated by JavaScript." to the
page.
6. Test your solution in a browser.
Code Explanation of Activity
• In the head, we use window.alert() to generate the popup.
We could have just used alert().
• We use document.write() to write to the screen at the
bottom of the page.
• We use onclick="document.body.style.backgroundColor =
'red'" and onclick="document.body.style.backgroundColor
= 'blue'" to add click handlers to the buttons.