Professional Documents
Culture Documents
JavaScript
Adding a script to your Pages
There are three places where you can put your JavaScripts ,
and a single XHTML document can use all three because there
is no limit on the number of scripts one document can contain:
In the < head > of a page: These scripts will be called when
an event triggers them.
In the < body > of a page: These scripts will run as the page
loads.
In an external file: If the link is placed inside the < head >
element, the script is treated the same as when the script
lives inside the head of the document waiting for an event to
trigger it, whereas if it is placed in the < body > element it
will act like a script in the body section and execute as the
page loads.
The Document Object Model
• In order to make a document more interactive, the
script needs to be able to access the contents of the
document and know when the user is interacting
with it.
• The script does this by interacting with the browser
by using the properties, methods and events set out
in the interface called the Document Object Model.
• properties of a document a script can retrieve and
which ones it can alter;
• methods that can be called to perform an action on
the document.
The anchor collection represents all the
anchors in a document that you can link
to ( < a > elements
with a name attribute).
The forms collection
contains all the < form >
tags in the document.
var userName
Assigning a Value to a Variable
When you want to give a value to a variable, you put the variable name first, then
an equal sign, and then on the right the value you want to assign to the variable.
Operators
document.write((a>b)&&(a<b));
document.write('</br>');
document.write((a>b)||(a<b));
document.write('</br>');
document.write(!(a<b));
</script>
</p>
</body>
</html>
Function
A function is made up of related code that performs
a particular task.
How to Define a Function
There are three parts to creating or defining a
function:
• Define a name for it.
• Indicate any values that might be required; these
are known as arguments.
• Add statements to the body of the function.
function calculateArea(width, height)
{
area = width * height
return area
}
How To Call a Function
To invoke a function somewhere later in the script, you would simply need to write the name of that
function as shown in the following code.
<html>
<head>
<script type = "text/javascript">
function sayHello()
{
document.write ("Hello there!");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello()" value = "Say Hello">
</form>
<p>Use different text in write method and then try...</p>
</body>
</html>
The Return Statement
Functions that return a result must use the return statement. This statement
specifies the value that will be returned to where the function was called. The
calculateArea() function, for example, returned the area of the rectangle:
</script>
<p>Set the variable to different value and then
try...</p>
</body>
</html>
Conditional Statements
2. if...else statement
The 'if...else' statement is the next form of control statement
that allows JavaScript to execute statements in a more
controlled way.
Syntax
if (expression)
{
Statement(s) to be executed if expression is true
}
else
{ Statement(s) to be executed if expression is false
}
Conditional Statements
<html>
<body>
<script type = "text/javascript">
3. Switch statement
The objective of a switch statement is to give an expression to evaluate and
several different statements to execute based on the value of the expression.
The interpreter checks each case against the value of the expression until a
match is found. If nothing matches, a default condition will be used.
Syntax
switch (expression)
{
case condition 1: statement(s)
break;
case condition 2: statement(s)
break;
...
case condition n: statement(s)
break;
default: statement(s)
}
Conditional Statements
3. Switch statement
<html>
<body>
<script type = "text/javascript">
</script>
Looping
var count = 0;
document.write("Starting Loop ");
document.write("Loop stopped!");
</script>
do . . . while
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.
Syntax
The syntax for do-while loop in JavaScript is as follows
do
{
Statement(s) to be executed;
}
while (expression);
Looping
<html>
<body>
<script type = "text/javascript">
var count = 0;
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
looping
for loop
The for statement executes a block of code a specified
number of times. You use it when you want to specify
how many times you want the code to be executed
(rather than running while a particular condition is
true/false).
Syntax
The syntax of for loop is JavaScript is as follows −
for (initialization; test condition; iteration statement)
{
Statement(s) to be executed if test condition is true
}
looping
<html>
<body>
<script type = "text/javascript">
var count;
document.write("Starting Loop" + "<br />");
</script>
<p>Set the variable to different value and then
try...</p>
</body>
</html>
Form Validation
</script>
<body>
<form name="myform" method="post"
action="http://www.javatpoint.com/javascriptpages/valid.jsp"
onsubmit="return validateform()" >
Thank you