0% found this document useful (0 votes)
24 views6 pages

Lec 8 JS

Uploaded by

umarmalikk64
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)
24 views6 pages

Lec 8 JS

Uploaded by

umarmalikk64
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/ 6

1.

Declaring function in JavaScript:


JavaScript provides us many built-in functions. These functions can be used in the code.
Similarly, user can define its own functions. The syntax to define a function is

function function-name (list of parameters)


{
Function body
}

Functions can be called by name. The syntax for calling a function is given below
Function-name (required parameters)
In JavaScript, usually we write functions and call these functions when some event occurs. The
most common events are
 onClick: when user clicks on some element
 onDblClick: when user double clicks some element
 onChange: when the focus is changes
 onFocus: when the focus is set
 onMouseOver: when the mouse is moved over an element
 onMouseOut: when the mouse is moved away from an element
 onSubmit: when a form is submitted
 onload: when the web page is loaded in the browser window
In the following example, we have declared a function. This function asks the user to enter its
name and then writes the user’s name on the browser screen. The function is called on the
onload function of the page.
<html>
<head>
<title>Untitled Document</title>
<script language="javascript">
function getName()
{
var name=prompt("Pleae enter your name",'name')
document.write("Welcome Mr. ",name)
}
</script>
</head>
<body onload="getName()">
</body>
</html>

2. Conditional Statements in JavaScript:


Conditional statements allow you to take different actions depending upon different
statements. There are two types of conditional statements you will learn about here:
If statement:if statements allow code to be executed when the condition specified is met; if the
condition is true thenthe code in the curly braces is executed. Here is the syntax for an if
statement:
if (condition)
{ statement }
if….else statement:When you have two possible situations and you want to react differently for
each, you can use an if...else statement. This means: “If the conditions specified are met, run
the first block of code; otherwiserun the second block.” The syntax is as follows:
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is false
}

3. Looping Statements in JavaScript:


Looping statements are used to execute the same block of code a specified number of times.
Following are the basic loops in JavaScript.
 A while loop runs the same block of code while or until a condition is true.
 A do while loop runs once before the condition is checked. If the condition is true, it will
continue to run until the condition is false. (The difference between a do and a do while
loop is that do while runs once whether or not the condition is met.)
 A for loop runs the same block of code a specified number of times (for example, five
times).
In the following example we have used the for loop and the do-while loop to execute a block of
code repetitively.
<html>
<head>
<title>JavaScript LOOP</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
for (var i = 0; i < 10; ++i)
{
document.write("Hello Dear!<br>")
}
var j=0
do
{
document.write("Pakistan")
j++
}
while(j<10)
</SCRIPT>
</body>
</html>

The output of the above program is shown below

Document object model

1. Dialog boxes in JavaScript:


JavaScript provides the ability to pick up user input or display small amounts of text to the user
by using dialog boxes. These dialog boxes appear as separate windows and their content
depends on the information provided by the user. JavaScript provides three types of dialog
boxes.
Alert box:An alert box is simply a small message box that pops up and gives the user some
information. An alert dialog box is mostly used to give a warning message to the users. When
an alert box pops up, the user will have to click "OK" to proceed. The syntax of an alert dialog
box is given below
alert(“message”)
In the following code, we have used alert box
<html>
<head>
<title>alert box</title>
<script language="javascript">
function alertme(message)
{
alert(message)
}
alertme("Welcome to my page")
</script>
</head>
<body>
<h1> Hello World!</h1>
</body>
</html>
In the above code, a function alertme() is declared which receives a message as parameter and
generates an alert box showing that message. The output of the code is show in the following
figure

Prompt box: A prompt box is often used if you want the user to input a value before entering a
page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to
proceed after entering an input value. If the user clicks "OK" the box returns the input value. If
the user clicks "Cancel" the box returns null.
Confirm box:A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If
the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
In the following code a function conName() is written. This function asks the user to enter its
name, then a confirm box is shown to get a confirmation whether the user wants to write its
name on the browser or not. If user confirms, a welcome message for the user is written on the
browser
<html>
<head>
<title>Confirm Box</title>
<script language="javascript">
function conName()
{
var name=prompt("Your Name please!")
if(confirm("Write on the webpage ?"))
document.write("Welcome Mr." + name)
else
document.write("Welcome MR. Anonymous")
}
conName()
</script>
</head>
<body>
</body>

The output of the above program is shown below

If user enters its name and press ok, the confirm box is displayed
Now, when user clicks the ok button, the user’s name is written on the browser

You might also like