Java Script
Java Script
Features of JavaScript
Programming Tool for interactive HTML pages Interpreted Language Saves time as validation is done on Client side Event-Driven Programming Detects Client browser, hence platform independent
<html> <head> </head> <body> <script type=javascript"> some statements </script> </body> </html>
What is a Variable?
Information is stored in variables. Its value can be change during the execution of script. It can be referenced by its name to see its value or to change its value.
Declaring Variables
There are two ways of declaring a variable. Implicit Declaration Explicit Declaration Implicit Declaration Declare variables by using its name directly in the script. For example : name=Tom Explicit declaration - Using var keyword For example : var name name=Tom
variable names are sequences of letters, digits, and underscores: start with a letter variables names are case-sensitive don't have to declare variables, will be created the first time used
var x= hello; var x=1;
Variant
Integer Float
Boolean
Char
Variant
User-defined functions
User Defined Functions
no return type for the function (since variables are loosely typed) no types for parameters (since variables are loosely typed) by-value parameter passing only (parameter gets copy of argument
Variable scope is limited if the first use of a variable is preceded with var, then that variable is local to the function Function definitions go in the HEAD HEAD is loaded first, so the function is defined before code in the BODY is executed
Using Arrays
<script type="text/javascript"> function show() { var names = new Array("Ramesh","Suresh","Ganesh); for(i =0;i<3;i++) { document.writeln(names[i]); } } </script> <body> <script language="JavaScript"> show(); </script> </body>
document object
Both IE and Netscape allow you to access information about an HTML document using the document object (Note: not a class!) document.write()
method that displays text in the page
document.URL
property that gives the location of the HTML document
document.lastModified
property that gives the date & time the HTML document was saved <script language= JavaScript> <!-document.writeln(<p><br> This page is located at +document.location+ <br>This page was last modified on +document.lastModified); //--> </script>
Create a HTML File to invoke the same <script language="javascript" src="First.js"></script> <FORM > <INPUT TYPE="BUTTON" NAME="b1" VALUE="What\'s your name" onClick=display()"> </FORM>
In lining of JavaScript
<html> <head> <title>Example</title> </head> <body>
Alert boxes
An alert box is a small window that pops up with a yellow warning sign and a (usually) important message. With JavaScript we can either show a message to the user either before a page loads or in response to a user action.
<script LANGUAGE=JavaScript>
alert (Welcome to the Test Site!); </script>
Confirm Boxes
Confirm box User will have to click either "OK" or "Cancel" to proceed
<form> <input type = ''button" value = "Click here to check the weather" onClick = "confirm(Itssunny today ' ) ; " > </form>
The onClick event handler means that when click on the button, a confirm box pops up.
The onload event is often used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information
Both the onload and onUnload events are also often used to deal with cookies that should be set when a user enters or leaves a page.
onLoad
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!-function yourMessage() { alert("Your first function!"); } // --> </script> </head> <body onLoad="yourMessage()"> </body> </html>
Example: The checkEmail() function will be called whenever the user changes the content of the field:
Example : onblur
<html> <head> <script type="text/javascript"> function upperCase() { var x=document.getElementById("fname").value document.getElementById("fname").value=x.toUpperCase() } </script> </head> <body> Enter your name: <input type="text" id="fname" onblur="upperCase()"> </body>
onSubmit
The onSubmit event is used to validate ALL form fields before submitting it.
Example: The checkForm() function will be called when the user clicks the submit button in the form. If the field values are not accepted, the submit should be cancelled.
The function checkForm() returns either true or false. If it returns true the form will be submitted, otherwise the submit will be cancelled: <form method="post" action="xxx.html" onsubmit="return checkForm()">
onSubmit
When calling functions parameters are sent by including them in the parentheses that follow the function name. The this statement passes a reference to the form object associated with the current form.
The return statement transmits a value back to the internal routine that called the onsubmit event handler.
OnSubmit
<script language="JavaScript"> function sendMe() { return confirm("Do you want to continue the submission?"); } </script> </head> <body> <form name="form1" method="POST" action="" onSubmit="return sendMe()"> <P><input type="text" name="inp1"> <input type="submit" value="Submit" > <input type="reset" value="Reset"></p> </form>
The browser displays a different image when the mouse is over the text or image.
To specify a mouse rollover the onMouseOver or the onMouseOut attributes into the appropriate tag.
onmouseover="document.pict.src ='images/B.GIF'"
onmouseout="document.pict.src='images/A.GIF'">
OnMouseDown
<html> <head> <script type="text/javascript"> function show_coords(event) { x=event.clientX y=event.clientY alert("X coords: " + x + ", Y coords: " + y) } </script> </head> <body onmousedown="show_coords(event)"> <p>Click in the document. An alert box will alert the x and y coordinates of the cursor.</p> </body> </html>
<!-- custom submit button --> <input type="button" value="Submit Form" onclick="document.forms[0].submit()" />
</form> </body> </html>
UserName <input type="text" name="name"/> <p> Address <input type="text" name="address"/> <p> Email : <input type="text" name="email"/> <p> <input type="submit" value="Register"/>
</form>
Example: String JavaScript object has length property and toUpperCase() method
Add properties to it
personObj.firstname="John"; personObj.lastname="Doe"; personObj.age=50;
Creating a Instance
Add a pre-defined function
function tellYourage(){ alert(The age is + this.age); } personObj.tellYourage=tellYourage;
Note that the following two lines of code are doing completely different things
personObj.tellYourage=tellYourage; personObj.tellYourage=tellYourage();
HTML DOM
The HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate HTML documents
All HTML elements, along with their containing text and attributes, can be accessed through the DOM. The contents can be modified or deleted, and new elements can be created.
The HTML DOM is platform and language independent It can be used by any programming language like Java, JavaScript, and VBScript
Use getElementsByName()
<html> <head> <script type="text/javascript"> function getElements() { var x=document.getElementsByName("myInput") alert(x.length + " elements!") } </script> </head> <body> <input name="myInput" type="text" size="20"><br /> <input name="myInput" type="text" size="20"><br /> <input name="myInput" type="text" size="20"><br /> <br /> <input type="button" onclick="getElements()" value="How many elements named 'myInput'?"> </body> </html>
Cookies - Set
function set_it() { var the_text="name=yourName&"; var toexpire= new Date("March 15, 2008"); var expdate="expires="+toexpire.toGMTString(); the_text+=expdate; var newtext=escape(the_text); document.cookie=newtext; }
Cookies-Read
function read_it() { if (document.cookie) { var var var var var mycookie=document.cookie; fixed_cookie= unescape(mycookie); thepairs= fixed_cookie.split("&"); pair1= thepairs[0]; pair2= thepairs[1];
Javascript is a scripting language which can be used in Web pages to do client side validations. It is supported by all the browsers.
It is also used to validate web pages to improve design and add functionality to web pages.