JavaScript
Introduction of JavaScript?
1. Java Script was designed to add interactivity to HTML pages
2. JavaScript is a scripting language
3. A scripting language is a lightweight programming language
4. JavaScript is usually embedded directly into HTML pages
5. It is used for client side operations like validation ,animation etc.
Basic Programming-
Variable−for declaring a variable you can follow the following syntax−
Var variable_name;
Note−javascript is a loosly typed programming language,means you can use a variable without
its declarion.by default it contains object type values but for conversion you canuse−
parseInt()
parseFloat()
parseDouble()
Operators
1. AssignmentOperator(=)
2. ArithmeticalOperator(+,−,*,/,%)
3. RelationalOperator(<,<=,>,>=,==,!=)
4. LogicalOperator(&&,||,!)
5. Increment/DecrementOperator(++,−−)
6. ConditionalOperator(?:)
Example
<html>
<head>
<scriptlanguage="javascript">v
ara,b,c;
a=10;
b=20;
c=a+b;
document.write("Sum:"+c);
document.write("<hr>");
</script>
</head>
<body>
</body>
</html>
SomeBuiltinFunctions-
1. alert()−this function is used for showing a message
2. prompt()−this method is used for reading any value
3. confirm()−this method is used for confirmation message
Que−ReadTwoValues&print the larges tone.
Decision Making Statements-They are used for making the decision inside your
program.The Decision Making Statements are−
1. if statement
2. switch statement
Que−Read three values & print the largestone
Que−Read two values & then print the following menu−
1. Add
2. Subtract
3. Multiply
4. Divide
Then read the choice(1−4)& print the result according to choice.
Loop–Loop statements are used for repeating a part of program during finite or infinite
times.It Is also known as iterative or repetitive statement.It could be class if ied into two
categories−
a) Entry Controlled Loop
b) Exit Controlled Loop
a) Entry Controlled Loop– This type of loop first checks the condition& if conditionis
Satisfied then execute the body of the loop.“forloop”&“while loop”belongstothiscategory.
b) Exit Controlled Loop– This type of loop execute at least one time either condition is
Satisfied or not.“dowhileloop” belongs to this category.
Que−WAP for calculating to the powern
Que−check give n number is prime or not?
Functions-
User Defined Functions in JavaScript will be like the following−
Function function_name([argument])
{
Bodyofthefunction[returnv
alue]
}
Events−Events are the actions which indicates happening of any operation.Some most
frequently used event sare−
Onload−fires when the page is loaded
Onunload−fires when the page unload from browser
onClick−fires when object is cliked
Onblur−when the focus goes
outOnfocus−when the focus comes in
Onchange−when the value is changed
Onsubmit−when the server side code fires
Onmouseover/onmouseout−Onkeypress−
when the keypressed
Events&Functions
Onload and onunload Example
<html>
<head>
<script language="javascript">
function first()
{
alert("WelcomeFriends");
}
Function sec()
{
alert("Bye−ByeFriends");
}
</script>
</head>
<body onload="first()" onunload="sec()">
</body>
</html>
onClickExample
<html>
<head>
<scriptlanguage="javascript">f
unctionfirst()
{
alert("WelcomeFriends");
}
</script>
</head>
<body>
<inputtype="button"value="ClickHere"onclick="first()">
</body>
</html>
onblur&onfocusExample
<html>
<head>
<scriptlanguage="javascript">f
unctionfirst()
{
b1.value="PleaseDo'ntLeaveMeAlone...";
}
functionsec()
{
b3.value="Ohh..IGotIt";
}
</script>
</head>
<body>
<inputtype="button"value="First"name="b1"onblur="first()">
<inputtype="button"value="Second"name="b2">
<inputtype="button"value="Third"name="b3"onfocus="sec()">
</body>
</html>
Onmouse out and onmouseover example
<html>
<head>
<script language="javascript">
function first()
{
b1.value="TouchMe..";
}
Function sec()
{
b1.value="DontTouch..";
}
</script>
</head>
<body>
<input type="button" value="TouchMe" name="b1" onmouseout="first()"
onmouseover="sec()">
</body>
</html>
Onkeypress Example
<html>
<head>
<script
language="javascript">varcou
nt=0;
Function first()
{
count++;
b1.value="YouPressKey.."+count+"times..";
}
</script>
</head>
<body>
<inputtype="button" value="ClickMe" name="b1" onkeypress="first()">
</body>
</html>
Working with form-
We can access the value of an input field of form through value property in javascript
.Example
<html>
<head>
<scriptlanguage="javascript">function
check(n)
{
if(n=="Dhananjay")
alert(n+"YouareGoodPerson");
else
alert(n+"YouareFraud");
}
</script>
</head>
<body>
<form name="f1">
<table width="500px"border="2"style="font−family:arial;font−size:14px">
<tr style="background−color:maroon;color:white">
<td colspan="2">ProgramForClickEvent</td>
</tr>
<tr>
<td>EnterYourName</td>
<td><inputtype="text"name="t1"></td>
</tr>
<tr>
<td></td>
<td><input type="button"value="Check"onclick="check(t1.value)"></td>
</tr>
</table>
</form>
</html>