Lab4
Lab4
Hamdard University
Lab # 04
JavaScript
Objectives:
Theory:
What is JavaScript?
JavaScript and Java are two different computer languages.
It is 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.
Syntax
<script>
Js code
</script>
JavaScript in <head> or <body>
You can place an unlimited number of scripts in an HTML document. Scripts can be in the <body> or in
the <head> section of HTML, and/or in both. It is a common practice to put functions in the <head>
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 <body>
In this example, JavaScript writes into the HTML <body> while the page loads:
<!DOCTYPE html> JavaScript can write directly into the HTML output
<html> stream:
<body> This is a heading
This is a paragraph.
<p> You can only use document.write in the HTML
JavaScript can write directly into the HTML output
output. If you use it after the document has loaded
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
<script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
</script>
<p>
You can only use
<strong>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.
</p>
</body>
</html>
External JavaScripts
Scripts can also be placed in external files. External files often contain code to be used 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 <script> tag:
My Web Page
A Paragraph.
On Click
My Web Page
My First External JavaScript
Note: myFunction is stored in an external
file called "myScript.js".
JavaScript Outputs:
Manipulating HTML Elements
To access an HTML element from JavaScript, you can use the document.getElementById(id) method.
Use the "id" attribute to identify the HTML element:
<script>
document.getElementById("demo").innerHTML="My
First JavaScript";
document.write("<p>My First JavaScript</p>");
</script>
</body>
</html>
Warning
Use document.write() only to write directly into the document output. If you execute document.write after
the document has finished loading, the entire HTML page will be overwritten:
<script>
function myFunction()
{
document.write("Oops! The document
disappeared!");
}
</script>
</body>
</html>
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:
<script>
document.getElementById("demo").innerHTML="Hello
Dolly";
document.getElementById("myDIV").innerHTML="How
are you?";
</script>
</body>
</html>
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 code.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Hello World!");
}
</script>
</head>
<body>
<button onclick="myFunction()">Try it</button>
</body>
</html>
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
<button onclick="myFunction('Harry
Potter','Wizard')">Try it</button>
<script>
function myFunction(name,job)
{
alert("Welcome " + name + ", the " + job);
}
</script>
</body>
</html>
Example:
document.getElementById("demo").innerHTML=myFunction(
4,3);
</script>
</body>
</html>
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
JavaScript Variables
<!DOCTYPE html> What a verynice day
<html>
<body>
<p id="demo"></p>
<p id="demoo"></p>
<script>
function myFunction()
{
txt1="What a very";
txt2="nice day";
txt3=txt1+txt2;
document.getElementById("demo").innerHTML=txt3
;
}
function myFunctions()
{
var x=5+5;
var y="5"+5;
var z="Hello"+5;
vardemoP=document.getElementById("demoo");
demoP.innerHTML=x + "<br>" + y + "<br>" + z;
}
</script>
</body>
</html>
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
If...else Statement
Use the if....else statement to execute some code if a condition is true and another code if the
condition is not true.
Syntax
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Example
If the time is less than 20:00, you will get a "Good day" greeting, otherwise you will get a "Good
evening" greeting
if (time<20)
{
x="Good day";
}
else
{
x="Good evening";
}
Good day
Syntax
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
{
code to be executed if condition2 is true
}
else
{
code to be executed if neither condition1 nor condition2 is true
}
Example
If the time is less than 10:00, you will get a "Good morning" greeting, if not, but the time is less
than 20:00, you will get a "Good day" greeting, otherwise you will get a "Good evening"
greeting:
if (time<10)
{
x="Good morning";
}
else if (time<20)
{
x="Good day";
}
else
{
x="Good evening";
}
Good morning
JavaScript Loops
Loops are handy, if you want to run the same code over and over again, each time with a
different value.
Often this is the case when working with arrays:
Instead of writing:
document.write(cars[0] + "<br>");
document.write(cars[1] + "<br>");
document.write(cars[2] + "<br>");
document.write(cars[3] + "<br>");
document.write(cars[4] + "<br>");
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
document.write(cars[5] + "<br>");
Example:
<!DOCTYPE html>
<html>
<body>
<script>
for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>
</body>
</html>
while loop
<script>
var i=11;
while (i<=15)
{
document.write(i + "<br/>");
i++;
}
</script>
Do while loop
<script>
var i=21;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
JavaScript Functions
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 code.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Hello World!");
}
</script>
</head>
<body>
<button onclick="myFunction()">Try it</button>
</body>
</html>
The variables and the arguments must be in the expected order. The first variable is given the
value of the first passed argument etc.
<button onclick="myFunction('Harry
Potter','Wizard')">Try it</button>
<script>
function myFunction(name,job)
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
{
alert("Welcome " + name + ", the " + job);
}
</script>
</body>
</html>
<p id="demo"></p>
<script>
function myFunction(a,b)
{
return a*b;
}
document.getElementById("demo").innerHTML=myFunction(
4,3);
</script>
</body>
</html>
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard University
JavaScript Objects
"Everything" in JavaScript is an Object: a String, a Number, an Array, a Date. In JavaScript, an object is
data, with properties and methods.
<script>
var person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";
document.write(person.firstname + " is " +
person.age + " years old.");
</script>
</body>
</html>
Lab Tasks:
Q1. Create a webpage that declaresa variable, assigns a value to it, displays the value, change the value,
and displays the value again and print the values on the webpage().
Q2. Create JS code to swap two values using third variable. And also without using third variable.
Q3. Create a webpage that inputs the name of the user using prompt box, welcomes the user when the
page is loaded, tells the length of the name entered and convert the name to the upper case.
Hint : to get input from user use the following:
var text = prompt('Enter your name ','Name' );
alert("Welcome "+ text);
Q4. Create an object Employee which has the following properties, id, name, designation, salary, phone
number and city.Create a webpage that inputs the data and on clicking the submit button display the
data in a dialogue box.
Learning outcomes: