FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
Lab #04
JavaScript
Objectives:
* To lear the basic structure of JavaScript.
* To learn the functions of Javascript
Theory:
What is JavaScript?
* JavaScript and Java are two different computer languages.
* [tis an object-oriented computer programming language commonly used to
create interactive effects within web browsers.
* JavaScript, is a text based object oriented computer programming language
meant to run as part of a web-based application and to create interactive
effects within web browsers.
‘* One of the three pillars of web development—the other two being HTML and
Css.
The
JavaScript in or
You can place an unlimited number of scripts in an HTML document. Scripts can be in
the or in the section of HTML, and/or in both. It is a common practice to
put functions in the section, or at the bottom of the page. This way they are all
in one place and do not interfere with page content.
JavaScript in
In this example, JavaScript writes into the HTML while the page loadsFACULTY OF ENGINEERING St
Hamdard
»)
)
od
SCIENCES AND TECHNOLOGY
University
JavaScript can write directly
into the HTML output stream:
You can only use
document.write
strong> in the HTML output.
If you use it after the document
has loaded (e.g. in a function),
the whole document will be
overwritten.
JavaScript can write directly into the
HTML output stream:
This is a heading
This is @ paragraph
You can only use document.write in the
HTML output. If you use it after the
document has loaded (e.g. in a function),
the whole document will be overwritten.
External JavaScripts
Scripts can also be placed in external files. Extemal files often contain code to be usedFACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
by several different web pages.External JavaScript files have the file extension .js.To
use an external script, point to the js file in the "src" attribute of the
My First Web Page
My First Paragraph.
After click
Oops! The document disappeared!
JavaScript Code
JavaScript code (or just JavaScript) is a sequence of JavaScript statements.
* Each statement is executed by the browser in the sequence they are written
© This example will manipulate two HTML elements:ff (\\
a)
we
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
My Web Page
Hello Dolly
How are you?
My Web Page
‘demo>A Paragraph.
spit
yDIV">A DIV.
JavaScript Functions
Declared functions are not executed immediately. They are "saved for later use", and
will be executed later, when they are invoked (called upon)
The function can be called directly when an event occurs (like when a user clicks a
button), and it can be called from "anywhere" by JavaScript codeFACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University