Академический Документы
Профессиональный Документы
Культура Документы
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("hey now brown cow");
</script>
</body>
</html>
------------------------------------------------------------------------------------------------------------------
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var name = prompt("Enter your name","Name");
//takes two
parameter..second parameter is default text in promptbox
document.write("Hello " + name);
</script>
</body>
</html>
______________________________________________________________________________
____
Variable
to define variable simply write var variablename = value
eg-
var z = -200;
document.write(z);
apples("akash","manu");
apples("fish","chicken");
apples("chicken","meat");
______________________________________________________________________________
____
Function call from another function
function doFirst()
{
document.write("I am first bro!");
}
function doSecond()
{
document.write("22222222!!");
}
function start()
{
doFirst();
doSecond();
}
start();
______________________________________________________________________________
____
Return statement
eg1-
<html>
<head>
</head>
<body>
<script type="text/javascript">
function addNumbers(a,b)
{
var c =a+b;
return c;
}
addNumbers(3,6); // It will simply add numbers but will not print it as there is
no command to print
</script>
<form>
</body>
</html>
______________________________________________________________________________
____
Event
There are many types of event.Event is a piece of code that gets executed
when some condition is fulfilled.Event can be written inside script tag or even
without script tag in body.
Eg-Without Script tag
<html>
<head>
</head>
<body>
<script type="text/javascript">
</script>
<form>
<input type="button" value="press me" onClick="alert('onClick
event');alert('second');"/>
</form>
</body>
</html>
here inside onClick and inside alert we used single quotes instead of double
quotes
<html>
<head>
</head>
<body>
<script type="text/javascript">
function hello()
{
alert("function hello");
}
</script>
<form>
<input type="button" value="press me" onClick="hello()"/>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
function hello()
{
alert("function hello");
}
</script>
<form>
<a href="akash.com" onMouseOver="alert('gametime');">keep mouse over
me</a>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
function hello()
{
alert("function hello");
}
</script>
<form>
<a href="akash.com" onMouseOut="alert('gametime');">keep mouse over
me</a>
</form>
</body>
</html>
Eg5-Onload event
<html>
<head>
</head>
<body onLoad="alert('loaded');">
<script type="text/javascript">
</script>
</body>
</html>
______________________________________________________________________________
____
Object
eg1-
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script type="text/javascript">
</script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function person(name,age)
{
this.name = name;
this.age = age;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(akash.name);
document.write(akash.age);
</script>
</body>
</html>
eg3-Object initializer
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
//OBJECT INITIALIZER -use this only when u want one or two objects otherwise
use constructor function
manu = {name:"aditya",age:28};
</script>
</head>
<body>
<script type="text/javascript">
</script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function people(name,age)
{
this.name = name;
this.age = age;
this.retire = calculate;
}
function calculate()
{
var num = 65 - this.age;
return num;
}
</body>
</html>
______________________________________________________________________________
____
Arrays
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
document.write(names +"<br>");
document.write(names[2]+"<br>");
var akash=["a","b","c","d"];
document.write(akash + "<br>");
fruits[0] = "apple";
fruits[1] = "banana";
fruits[3] = "orange";
document.write(fruits+"<br>");
//dynamic array
</script>
</body>
</html>
______________________________________________________________________________
____
Array properties and methods
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
// number of
document.write(anime + "<br>");
document.write(bodyparts +"<br>");
bodyparts.reverse();
//reverse an array
document.write(bodyparts +"<br>");
bodyparts.sort();
document.write(bodyparts);
</script>
</body>
</html>
______________________________________________________________________________
____
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
for(i=0;i<3;i++)
{
names[i] = prompt();
}
</script>
</body>
</html>
______________________________________________________________________________
____
Printing dynamic array
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
for(i=0;i<names.length;i++)
later
{
document.write(names[i] + "<br>");
}
</script>
</body>
</html>
______________________________________________________________________________
____
Associative Arrays
Arrays with named index arer called associative arrays
It means using array without index numbers but using names or strings
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
document.write(akash["name"]);
</script>
</body>
</html>
______________________________________________________________________________
____
Math object
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
document.write(Math.E);
</script>
</body>
</html>
______________________________________________________________________________
____
Set interval function
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function akash()
{
document.write("Akash is the best ");
}
setInterval("akash()",1000); //second parameter is in millisecond
</script>
</body>
</html>
______________________________________________________________________________
____
Date object
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
document.write(now);
function clock()
{
var time = new Date();
var hours = time.getHours();
var mins = time.getMinutes();
var seconds = time.getSeconds();
<script type="text/javascript">
var x = document.forms[0].length;
form
</script>
</body>
</html>
______________________________________________________________________________
____
Forms1
Accessing element of form can be done in two ways using arrays and using
name directly
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<form name="firstform">
Username: <input type="text" name="username"/>
Password: <input type="text" name="password"/>
<input type="submit" value = "Submit!"/>
<script type="text/javascript">
var x = document.forms[0].elements[0].name;
document.write(x);
var x = document.firstform.username.name;
document.write(x);
</script>
</body>
</html>
______________________________________________________________________________
____
Validation[checkbox]
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function validation()
{
if(document.firstform.thebox.checked)
alert("It is checked");
else
alert("Not checked");
}
</script>
</head>
<body>
<form name="firstform">
<input type ="checkbox" name = "thebox"/>
<input type="button" value="press me!" onClick="validation()" />
</body>
</html>