Javascript 2
Javascript 2
2.Confirm()
<html>
<script language="javascript">
function show_confirm()
{
var x;
if (confirm("Do you want to save changes?")==true)
{
x="Data saved successfully!";
} else {
x="Save Cancelled!";
}document.getElementById("eid").innerHTML=x;
}
</script>
<body>
<input type="button" onclick="show_confirm()" value="Display confirm box">
<p id="eid"></p>
</input>
</body>
</html>
3.Prompt()
<html>
<head>
<script language="javascript">
function show_prompt()
{
var x=prompt("enter your mail id");
document.write("your mail id is "+x);
}
</script>
</head>
<body>
<input type="button" onclick="show_prompt()" value="prompt_box" ></input>
</body>
</html>
4.Switch()
<html>
<head>
</head>
<body>
var num=1;
switch(num){
case 1:
document.write("Red");
break;
case 2:
document.write("Green");
break;
case 3:
document.write("Blue");
break;
default:
document.write("Black");
</script>
</body>
</html>
5.for-in loop
<html>
<body>
<script type="text/javascript">
text+=person[x];
document.write(text);
document.write("<br>");
</script>
</body>
</html>
6.Logical operators
<html>
<head>
<title>Operator Example</title>
</head>
<body>
<script language="JavaScript">
<!--
var userID ;
var password;
userID = prompt("Enter User ID"," ");
password = prompt("Enter Password"," ");
<html>
<body>
<script language="javascript">
function about_you(name,age,qualification){
document.write("All about you<br>");
document.write("your name is"+name+"<br>");
document.write("your age is"+age+"<br>");
document.write("your qualification is"+qualification+"<br>");
}
about_you('ramu',36,'MCA'); //function call
about_you('geetha',38,'BSC');//function call
</script>
</body>
</html>
JavaScript Objects
A JavaScript object is an entity having state and behavior (properties and method).
For example: car, pen, bike, chair, glass, keyboard, monitor etc.
JavaScript is template based not class based. Here, we don't create class to get the
object. But, we directly create objects.
1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)
object={property1:value1,property2:value2.....propertyN:valueN}
8.<html>
<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
</script>
</html>
Output :
102 Shyam Kumar 40000
9.<html>
<script>
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
</script>
</html>
Output :
Here, you need to create function with arguments. Each argument value can be
assigned in the current object by using this keyword.
10.<html>
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
</script>
</html>
Output :
We can define method in JavaScript object. But before defining method, we need
to add property in the function with same name as method.
11.<html>
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
this.changeSalary=changeSalary;
function changeSalary(otherSalary){
this.salary=otherSalary;
e.changeSalary(45000);
</script>
</html>
Output :
JavaScript JSON
Points to remember
JSON Syntax
{
"First_Name" : "value";
"Last_Name": "value ";
}
2. While working with JSON object in .js or .html file, the syntax will be like:
var varName ={
"First_Name" : "value";
"Last_Name": "value ";
}
Let's see the list of JavaScript JSON method with their description.
Method Description
Let's see an example to convert string in JSON format using parse() and stringify()
method.
12.<html>
<body>
<script>
</script>
</body.
</html>
OutPut:
Program:
<!DOCTYPE html>
<html>
<head>
<title>TEXT-SHRINKING TEXT-GROWING</title>
</head>
<body>
<div id="h"></div>
<script>
var v = 0, f = 1,t="TEXT-GROWING",color;
function a()
if(f==1)
v+=5,color="red";
else
v-=5,color="blue";
if(v==50)
f = 0, t="TEXT-SHRINKING";
if(v==5)
f = 1, t="TEXT-GROWING";
c();
function c()
setTimeout(a,300);
c();
</script>
</body>
</html>