Chap 04 - Intro To Client-Side Scripting
Chap 04 - Intro To Client-Side Scripting
Introduction to
Client-Side Scripting
( JavaScript )
Prepared for:
CSC264 – Introduction to Web and Mobile Application
OVERVIEW OF THIS CHAPTER
WHY?
WHY?
WHY?
The scripts involved will be processed by the browser (client), not the server
JavaScript:
All JavaScript scripts will be embedded in HTML documents, either by:
2. Linking to a Javascript document as an external file:
The external JavaScript file will have an extension .js
To use the external JavaScript, the src attribute can be
used
<script src=“myScript.js”></script>
JAVASCRIPT: VARIABLES
Variables:
Unlike HTML, JavaScript is case-sensitive
JavaScript’s variable can hold a value of any data type
For example:
var fullName;
var age;
var _gender;
var drink_option;
JAVASCRIPT: DATA TYPES
Data Types:
JavaScript allows you to work with three primitive data types:
1. Numbers:
In JavaScript, there is no distinction between integer values and
floating-point values:
All numbers in JavaScript are represented as floating-point values
For example: 50, -100, 120.78
2. Strings of text:
Each string must be enclosed in either:
Double quotation “ ”
Single quotation ‘ ’
For example:“Marc Jacobs”,“My name is ”
3. Boolean value:
The value can either be true or false
JAVASCRIPT: COMMENTS
Comments:
Will be ignored when the program is executed
To make a single comment in JavaScript:
/*This is a comment
This is also a comment*/
JAVASCRIPT: OUTPUT
Output Operation:
There are different ways to display data in JavaScript:
1. Writing into the HTML output:
The output will be displayed as HTML output
The syntax used is:
document.write(............);
1st way:
JavaScript is placed at the
bottom of the body section
JAVASCRIPT: OUTPUT
Output Operation:
There are different ways to display data in JavaScript:
1. Writing into the HTML output:
2nd way:
JavaScript is placed as a function
at the head section
JAVASCRIPT: OUTPUT
Output Operation:
There are different ways to display data in JavaScript:
2. Writing into an alert box:
The output will be displayed on a dialogue box which will pop up
on the screen
The syntax used is:
alert(............);
JAVASCRIPT: OUTPUT
Output Operation:
There are different ways to display data in JavaScript:
3. Writing into the browser console:
The output will be displayed on the console of the browser
(press F12 on your browser to get to its control)
The syntax used is: console.log(............);
JAVASCRIPT: OUTPUT
Output Operation:
There are different ways to display data in JavaScript:
4. Writing into an HTML element:
The output will be displayed as the content of an HTML element
Will use an ID of the chosen HTML element
Use this when you want to change the content of an HTML element
The syntax used is:
document.getElementById(“htmlElementID”).innerHTML = ........ ;
JAVASCRIPT: OUTPUT
Output Operation:
There are different ways to display data in JavaScript:
4. Writing into an HTML element:
Use this when you want to change the content of an HTML element
JAVASCRIPT: OUTPUT
Output Operation:
When you have many data or strings to display:
You can use“+”to concatenate them
JAVASCRIPT: DIALOG BOX
Dialog Box:
In JavaScript, there are 3 kinds of pop-up dialog boxes for interacting
with the users:
1. Alert Box:
Will open a dialog box that displays the parameter and an OK
button
Often used if you want to make sure information comes
through to the user:
Because it will wait for the user until they press the OK
button
The syntax used is:
alert(displayParameter);
JAVASCRIPT: DIALOG BOX
JAVASCRIPT: DIALOG BOX
Dialog Box:
In JavaScript, there are 3 kinds of pop-up dialog boxes for interacting
with the users:
2. Prompt Box:
Will open a dialog box and display the parameter, along with a
text box and two buttons (OK and Cancel)
Often used if you need an input from the user
The second parameter is for a default response (optional):
Will provide an initial value as input
The syntax used is:
prompt(displayParameter,“defaultResponse”);
JAVASCRIPT: DIALOG BOX
JAVASCRIPT: DIALOG BOX
JAVASCRIPT: DIALOG BOX
Dialog Box:
In JavaScript, there are 3 kinds of pop-up dialog boxes for interacting with the
users:
3. Confirm Box:
Will open a dialog box and display the parameter and two buttons
(OK and Cancel)
Will return a Boolean value, depending on which button was pressed
(OK: true or CANCEL:false)
Is often used if you want the user to verify or accept something
The syntax used is
confirm(displayParameter);
JAVASCRIPT: DIALOG BOX
JAVASCRIPT: ESCAPE SEQUENCES
Escape Sequences:
Are used to display special characters into a string:
What would happen if the following is written?
Function Description
parseFloat() Convert value into number of type float
parseInt() Convert value into number of type integer
Number() Convert value into a number
JAVASCRIPT: PARSING
JAVASCRIPT: STRING HANDLING
String Handling:
There are many methods that can be used on a string:
EXAMPLE
JAVASCRIPT: NONSTANDARD METHOD
Non-standard Method:
Below are some JavaScript method that can also be used:
Instead of using HTML code <b></b> for bold:
.bold()
Instead of using HTML code <font color=“ ”></font> for font colour:
.fontcolor(“....”)
.link(“....”)
EXAMPLE
JAVASCRIPT: HTML CODE
HTML Code:
Can be included in JavaScript (by writing it as HTML output)
However, it must be enclosed inside a quotation mark (“ ”or ‘ ’)
JAVASCRIPT: HTML CODE