0% found this document useful (0 votes)
5 views5 pages

Unit 5 Read Me

Read me

Uploaded by

Hsu Myat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Unit 5 Read Me

Read me

Uploaded by

Hsu Myat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Unit 5 Read me

Snap recap
1. List the elements of HTML you have learnt so far
2. What do you think id s limitation of HTML script?

Learning objective

You will learn about


The importance of JavaScript
Writing a javascript program
Using an external JavaScript file
The object model in JavaScript
The document.write() method
JavaScript variables and operators
Using the window.alert() and window.confirm() methods
Using parseInt() and parseFloat() to do arithmetic and calculations.

Introduction
HTML is a very limited document formatting language. It is based on tags that instruct the
browser how to display text or an image. As such , HTML is limited to a static, one-way
interaction with the user.

To add functionality to your web page you need the scripting language. JavaScript is one of the
most popular scripting languages for the internet. It works in all major browser.

JavaScript
JavaScript is a scripting language with a very simple syntax. It is used to add interactivity to your
webpages.

JavaScript provides web page authors with the power to reach a very high level of interaction
between the user and the document.

The importance of JavaScript


Javascript is used because
It helps to add interactive elements to HTML pages.
It is a scripting language which is also a lightweight programming language.
Everyone can use JavaScript without purchasing a licence.
It is supported by all major browsers.

Writing a JavaScript program


The <SCRIPT> tag may be placed in either the head or body of a HTML document.

Using an external JavaScript file

If you want to run the same JavaScript on several pages without having to write the same script
on every page. You can write a JavaScript in an external file.

For example

<SCRIPT language= “JavaScript” src=” scriptFILE.js”>

[ additional JavaScript statements]


<SCRIPTS/>

Document.write() method

This document.write() method is used for displaying the text on the browsers window. It uses ab
object called “document”. Which refers to the current document on the browsers

Using HTML tags in JavaScript

HTML tags can use also be included in the JavaScript code to generate HTML elements that will
be displayed in the browser window. Once the browser receives a string from the
document.write() method.

Rules for writing the JavaScript

- The scripting language has to be written within <SCRIPT> … </SCRIPT> tags in s HTML
document.
- The <SCRIPT> … </SCRIPT> tags can either be written in the <HEAD> or <BODY> of HTML
document
- A program can have more than one of the <SCRIPT> … </SCRIPT> tags
- JavaScript is a case sensitive language
- Using a semicolon at the end of the statement is optional. Using semicolons makes it
possible to write multiple statement on one line.

Adding comment to JavaScript code

There are two different types of comment in JavaScript:

Single one line comments


Multiple line comments
Eg. //This is comment line 1
/*This is comment line 1
This is comment line 2*/

JavaScript variables

Variables are the name assigned to a memory location that can be used for storing data.
Its valuables can change during the execution of a script. A variable can have a short
name, like x, or more descriptive name, like Fname. These variables, by default, hold on
one value at a time. If you try to assign another value then the previous one is
overwritten.

The rules below should be followed when assigning a name to a variable:


A variable name is case sensitive (y and Y are two different variable)
It can contain letters (A…Z, a…z), digits (0…9) and underscore (_) only.
It must start with a letter or an underscore character
It cannot contain empty space (white space)
It cannot have more than 255 characters
It cannot be one of the language’s reserved words (keyword), such as var, alert, etc.

Declaring variables in JavaScript

You can declare a variable in JavaScript by using var along with the variable name.

JavaScript operator

Arithmetic operators
Comparison/Relational operators
Logical operators
String operators

Comparison operator

Comparison operators are used to test if two variable relate to each other in the
specified way. These returns values which are either True or False.

Logical operator

The logical operators perform logical operations on variables.

String operators

A string is a set of characters. To join two or more string values together, you use the
plus ‘+’ operator. String concatenation operator.
Methods for the window object

Window.alert () method

window.alert () is a method of the window object. It is the simplest dialog box used to
display a short message to the user in a separate small window.
The syntax for the alert box is:
Window.alert (message)

Window.confirm() method

The confirm box includes both OK and Cancel buttons. Thus method returns the value
True if OK is oressed and False if Cancel is pressed.

The syntax for the confirm box is

Window.confirm (message)

Or

Confirm (message)

Window.prompt() methid

The method is used for obtaining input from the user. It displays an Explorer User
Prompt dialog box with a message and and an input field.

The syntax for the prompt box is

Window.prompt (message, default value)

Or

Prompt(message, default value)

ParseInt and parseFloat() methods

parseInt() and parseFloat() methods in JavaScript are used to convert a non-number


value into numbers, parseInt() converts a string value into intergers (numbers without
decimal places) and parseFloat() converts a string value into floating numbers (numbers
with decimal values)

You might also like