Javascript
Javascript
lecture 5 2
How to put a JavaScript code into
an HTML page
Use the <script> tag (also use the type
attribute to define the scripting language)
In the HTML page itself:
<html>
<head>
<script language=“javascript”>
// JavaScript code
</script>
</head>
lecture 5 3
Scripts can be in the either <head>
section or <body> section
Can implement in 3 ways
◦ Within the body tag
◦ Inside head tag
◦ In an external file (with .js externsion)
Placing scripts in the Head
<html>
<head>
<script type="text/JavaScript">
....
</script>
</head>
As a file, linked from the HTML page:
<body>
<script language=“javascript” src=“script.js”>
</script>
</body>
Script.js file
document.write(“hello world”);
Placing scripts in the body
<HTML>
<HEAD>
<TITLE>A First Script</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE = “JavaScript”>
document. write("Hello World")
</SCRIPT>
</BODY>
</HTML>
A Simple Program: Printing a Line of
Text in a Web Page
Correct method call syntax:
◦ object.method( “string”, “[additional arguments]” );
document.writeln( “<H1>argument</H1>”
);
◦ Case-sensitive, like all JavaScript functions
◦ Uses the writeln method in the browser’s document
object
◦ Prints the string, which can consist of any text and
HTML tags
◦ String must be surrounded by quotation marks (“…”)
Statement terminators
◦ All statements must end with semi-colons (;)
lecture 7 8
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0
Outline
Transitional//EN">
2 <HTML>
3 <!-- Fig. 8.2: welcome.html -->
4
5 <HEAD>
6 <TITLE>Printing a Line with Multiple
Statements</TITLE>
7
8 <SCRIPT LANGUAGE = "JavaScript">
9 document.write( "<FONT COLOR='magenta'><H1>Welcome to " );
10 document.writeln( "JavaScript
Programming!</H1></FONT>"
11 </SCRIPT> );
12
13 </HEAD><BODY></BODY>
14 </HTML>
10
JavaScript Comments
1. Alert box,
2. Confirm box,
3. Prompt box.
Alert Box
syntax
alert("sometext")
example
<html>
<head>
<body>
<script type="text/javascript">
alert("Hello world")
</script>
</head>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("I am an alert box!");
}
</script>
</head>
<body>
<input type="button" onclick="show_alert()"
value="Show alert box" />
</body>
</html>
Confirm box
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button");
if (r==true)
{
document.write(“congratulations!");
}
else
{
document.write(“bad of you!");
}
}
</script>
</head>
<body>
<input type="button" onclick="show_confirm()" value="Show confirm box" />
</body>
</html>
Prompt Box
<script type="text/javascript">
var y=prompt("please enter your name")
alert(y)
</script>
</head>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Please enter your name",“jane");
if (name!=null && name!="")
{
document.write("Hello " + name + "! How are you
today?");
}
}
</script>
</head>
<body>
<input type="button" onclick="show_prompt()"
value="Show prompt box" />
</body>
</html>
<html>
<head>
<script language="JavaScript">
var n = prompt("Check your number", "Type your number here");
n = parseInt(n);
if (n == 0)
{
document.write(n)
alert("The number is zero");
}
else if (n%2)
{
document.write(n)
alert("The number is odd");
}
else
{
document.write(n)
alert("The number is even");
}
</script>
</head>
JavaScript Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder of a division)
++ Increment
-- Decrement
String Operators
In JavaScript, a string is simply a piece of text.
Operator Description
= Assignment
function popupalert()
{
alert('This is an alert box.');
}
Popupalert();
Calling a function
To call a function that takes an argument,
you must supply the argument. If you
know the value of the argument, you can
provide it:
<html>
<head>
<script language="JavaScript">
function test()
{
document.write("Hi there");
}
test();
</script>
</head>
</html>
<html>
<head>
<script language="JavaScript">
function area(w1, w2, h){
var area=(w1+w2)*h/2
alert(area+" sq ft")
}
area(2,3,7)
area(5,7,4)
area(3,2,1)
</script>
</head>
</html>
<html>
<head>
<Script Language="JavaScript">
function showAddress()
{
document.write(“170-90200 Kitui Kenya ");
}
</Script>
<Script Language="JavaScript">
showAddress();
</Script>
</head>
</html>
<html>
<head>
<title>Module two students</title>
<Script Language="JavaScript">
function welcomeMessage()
{
document.write("<b>Welcome to module two class!</b>");
}
</Script>
</head>
<body>
<Script Language="JavaScript">
welcomeMessage();
</Script>
</body>
</html>
<html>
<head>
<Script Language="JavaScript">
function circleArea(radius)
{
var area;
area = radius * radius * 3.14159;
document.write("The area of the circle is ", area);
}
</Script>
<Script Language="JavaScript">
circleArea(7)
</Script>
</head>
</html>
Calculating Sum of two numbers
<html>
<head>
<script language="JavaScript">
function sum(a,b)
{
var sum;
sum=a+b;
document.write("The sum is ", sum);
}
</script>
<script language="JavaScript">
var num1,num2;
num1 = 3;
num2 = 2;
sum(num1, num2);
</Script>
</head>
<html>
<html>
<head>
<script type="text/javascript">
function product (a,b)
{
return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3));
</script>
</body>
</html>
Using Prompt to calculate the area
of a rectangle
<html>
<head>
<Script Language="JavaScript">
function rectangleArea(length, height)
{
var area;
area = length * height ;
document.write("The area of the rectangle is ", area);
}
</Script>
<Script Language="JavaScript">
var L, H;
L = prompt("enter L",3);
H = prompt("enter H",2);
rectangleArea(L, H);
</Script>
</head>
<html>
Functions that return a value
<html>
<head>
<script type="text/javascript">
function product(a,b)
{
a=window.prompt("enter a value",4);
b=window.prompt("enter a vlaue",6);
return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product());
</script>
</body>
</html>
Thankyou…