Begin scripting

12
Begin Scripting! Debjani Roy Induction Training

Transcript of Begin scripting

Page 1: Begin scripting

11

Begin Scripting!Debjani Roy

Induction Training

Page 2: Begin scripting

2

The Document Object Model

Page 3: Begin scripting

3

Sample Form HTML<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>Name</title>

<style>

<!--

p { font-family: Arial; font-size: 10pt; color: #000000 }

-->

</style>

</head>

<body>

<form method="POST" name="myForm">

<p>

Name:&nbsp;&nbsp;&nbsp;

<input type="text" name="name" value="" width=20>

</p>

<p>

<input type="submit" value="Submit" name="B1">

<input type="reset" value="Reset" name="B2">

</p>

</form>

</body>

</html>

Page 4: Begin scripting

4

Form

Page 5: Begin scripting

5

Sample Script Functions: Scripts are written using a function. A function is a block of code that aims to

perform a set of related actions.

Syntax:function function_name(){Script code goes here}

The keyword “function” should be in lower case

Functions are written between the <script> </script> tags

<script language="javascript" type="text/javascript">

function function_name(){Script code goes here}

function function name(){Script code goes here}

</script>

Page 6: Begin scripting

6

Variables Variables are containers that hold values. You can declare variables

and store values in them

Declare variable:var fullname;

Assign value to variable:fullname=“Tom”;

Declare and assign value to a variable:var fullname=“Tom”;

The length of a variable can be obtained by the length property:len=fullname.length;

Page 7: Begin scripting

7

Associating a Function with an Event A function is called by associating the function with the event handler

of the object.

Example:<input type="submit" value="Submit" name="submit" onclick="checkValues()">

The function in the script tag:<script language="javascript" type="text/javascript">function checkValues(){

// some code goes here}</script>

Page 8: Begin scripting

8

Script for function checkvalues() <script language="javascript" type="text/javascript">

function checkValues()

{

var fullnametxt;

var fullnamelength;

fullnametxt=document.myForm.name.value;

if(fullnametxt=="")

{

var error="Please enter your name";

alert(error);

document.myForm.name.focus;

}

}

</script>

Page 9: Begin scripting

9

Associate function with an event

<input type="submit" value="Submit" name="submit" onclick="checkValues()">

Page 10: Begin scripting

10

Execute the Script

Page 11: Begin scripting

11

Refine the Script<script language="javascript" type="text/javascript">

function checkValues()

{

var fullnametxt;

var fullnamelength;

fullnametxt=document.myForm.name.value;

if(fullnametxt=="")

{

var error="Please enter your name";

alert(error);

document.myForm.name.focus;

}

else

{

alert("hello "+fullnametxt);

}

}

</script>

Page 12: Begin scripting

12

Execute the Script