Notes (Scripting Language)
Notes (Scripting Language)
Table of Content
• Brief Introduction
• Embedding JavaScript in HTML
• Statements/Expressions
• Functions - Defining - Calling
• Event Handlers
Introduction
• JavaScript is Netscape’s cross-platform, object-
based scripting language for client and server
applications
• JavaScript is not Java. They are similar in some
ways but fundamentally different in others.
JavaScript and Java
• The JavaScript resembles Java but does not
have Java's static typing and strong type
checking.
• JavaScript supports most Java expression syntax
and basic control-flow constructs.
• JavaScript has a simple, instance-based object
model that still provides significant capabilities.
JavaScript Types
There’re 2 types:
* Navigator’s JavaScript, also called client-side
JavaScript
* LiveWire JavaScript, also called server-side
JavaScript
Why Study JavaScript?
<SCRIPT>
<!-- Begin to hide script contents from old browsers.
JavaScript statements...
// End the hiding here. -->
</SCRIPT>
Famous “Hello World” Program
<html>
<body>
<script language="JavaScript">
document.write(“Hello, World!”)
</script>
</body>
</html>
JavaScript code in a file
• The SRC attribute of the <SCRIPT> tag lets you
specify a file as the JavaScript source (rather
than embedding the JavaScript in the HTML).
• This attribute is especially useful for sharing
functions among many different pages.
Example
<HEAD>
<TITLE>My Page</TITLE>
<SCRIPT SRC="common.js">
………….
</SCRIPT>
</HEAD>
<BODY>
………….
Statements
• To use an external script, put the name of the script file in the src
(source) attribute of a <script> tag
External JavaScript Advantages
• Before 2015, using the var keyword was the only way to declare a
JavaScript variable.
• The 2015 version of JavaScript (ES6) allows the use of the const
keyword to define a variable that cannot be reassigned, and the let
keyword to define a variable with restricted scope.
• Comparison and Logical operators are used to test for true or false.
• Comparison operators are used in logical statements to determine
equality or difference between variables or values.
• Given that x = 5, the table below explains the comparison operators
Switch Statement
• Function names can contain letters, digits, underscores, and dollar signs (same
rules as variables).
• HTML DOM methods are actions you can perform (on HTML
Elements).
• HTML DOM properties are values (of HTML Elements) that you can
set or change.
The DOM Programming Interface
• The HTML DOM can be accessed with JavaScript (and with other
programming languages).
• In the DOM, all HTML elements are defined as objects.
• The programming interface is the properties and methods of each
object.
• A property is a value that you can get or set (like changing the
content of an HTML element).
• A method is an action you can do (like add or deleting an HTML
element).
• In the example above, getElementById is a method, while innerHTML
is a property.
• The most common way to access an HTML element is to use the id of
the element.
• In the example above the getElementById method used id="demo" to
find the element.
• The innerHTML property can be used to get or change any HTML
element, including <html> and <body>.
HTML DOM Document
• The HTML DOM document object is the owner of all other objects in
your web page.
• The document object represents your web page.
• If you want to access any element in an HTML page, you always start
with accessing the document object.
• Below are some examples of how you can use the document object to
access and manipulate HTML.
HTML DOM Elements
• Often, with JavaScript, you want to manipulate HTML elements.
• To do so, you have to find the elements first. There are several ways
to do this:
• Finding HTML elements by id
• Finding HTML elements by tag name
• Finding HTML elements by class name
• Finding HTML elements by CSS selectors
• Finding HTML elements by HTML object collections
HTML Element by Id
• The easiest way to find an HTML element in the DOM, is by using the
element id.
• This example finds the element with id="intro":
HTML Elements by Tag Name
HTML DOM - Changing CSS
• To change the style of an HTML element, use this syntax:
• document.getElementById(id).style.property = new style