Professional Documents
Culture Documents
JavaScript Functions:
Function names can contain letters, digits, underscores, and dollar signs (same rules as
variables).
You can use the same code with different arguments, to produce different results.
calling function
Defining a function does not execute it. Defining it names the function and specifies what to do
when the function is called.
Calling the function actually performs the specified actions with the indicated parameters.
function_name() or function_name(parameters)
1
Unit 5 : JavaScript Functions
return statement
When JavaScript reaches a return statement, the function will stop executing.
If the function was invoked from a statement, JavaScript will "return" to execute the code after
the invoking statement.
Functions often compute a return value. The return value is "returned" back to the "caller":
function function_name()
{
//code to be executed.
return value;
}
Eg.
function myFunction(g1, g2) {
return g1 / g2;
}
Parameters are essentially passed to functions by value — so if the code within the body of a
function assigns a completely new value to a parameter that was passed to the function, the
change is not reflected globally or in the code which called that function.
When you pass an object as a parameter, if the function changes the object's properties, that
change is visible outside the function,
When you pass an array as a parameter, if the function changes any of the array's values, that
change is visible outside the function.
function myFunc(theObject) {
theObject.make = "Toyota";
}
myFunc(theObject); //function call
function myFunc(theArr) {
theArr[0] = 30;
}
2
Unit 5 : JavaScript Functions
Page Redirection
There may be a situation where you clicked a URL to reach a page X but internally you were
directed to another page Y. It happens due to page redirection. A redirect in JavaScript is a
technique that directs search engines and people to a different URL than the original. The
rerouted page may be located on the same or a different server. It might also take place on the
same or separate websites.
A web page can be redirected in a number of ways. The window.location object is used by
almost all functions. The most popular redirection techniques are location.href and
location.replace().
It may be used to obtain the current URL or web address's address. Without the window prefix,
the window.location object can be written.
One of the most often used is window.location objects. It's used to create a new document
from an existing one. We may give a new URL to this method, and it will initiate an HTTP
redirect in JavaScript. It differs from href in that it deletes the current document from the
document's history, making it impossible to return to the original.
Syntax
window.location.replace("new URL")
or
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.
3
Unit 5 : JavaScript Functions
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.
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.
Form Validation
Basic form validation can be applied with 2 different ways.
function validateform()
{
var name=document.myform.name.value;
var password=document.myform.password.value;
4
Unit 5 : JavaScript Functions
if (name==null || name=="")
{
alert("Name can't be blank");
return false;
}
else if(password.length<6)
{
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
<body>
<form name="myform" method="post" action="abc.html" onsubmit="return vali
dateform()" >
Name: <input type="text" name="name"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>
<script type="text/javascript">
function matchpass()
{
var firstpassword=document.f1.password.value;
var secondpassword=document.f1.password2.value;
if(firstpassword==secondpassword)
{
return true;
}
else{
alert("password must be same!");
return false;
}
}
</script>
5
Unit 5 : JavaScript Functions
<script>
function validate(){
var num=document.myform.num.value;
if (isNaN(num)){
document.getElementById("numloc").innerHTML="Enter Numeric value only";
return false;
}else{
return true;
}
}
</script>
<form name="myform" onsubmit="return validate()" >
Number: <input type="text" name="num"><span id="numloc"></span><br/>
<input type="submit" value="submit">
</form>
Using RegExp
A regular expression could be defined with the RegExp () constructor, as follows −
var pattern = new RegExp(pattern, attributes);
or
var pattern = /pattern/attributes;
Ex:
<html>
<head>
<title>JavaScript RegExp test Method</title>
</head>
6
Unit 5 : JavaScript Functions
<body>
<script type = "text/javascript">
var str = "Javascript is an interesting scripting language";
var re = new RegExp( "script", "g" );
Character Description
[...] Any one character between the brackets.
7
Unit 5 : JavaScript Functions
\s Whitespace character
\d a digit (0-9)
\D a non-digit
\W a non-word character
function check()
{
var n=document.getElementById("txtname").value;
var pin=document.getElementById("txtpincode").value;
p=/\d+$/g;
if(n.match(p))
{
alert("Enter valid name");
return false;
}
else if(pin.length>8 || pin.length<8)
{
alert("Please enter 8 digits of pincode");
return false;
}
else
{
// return true;
}
}