JS Events Nad Onwards
JS Events Nad Onwards
com/javascript
Event Handling is a software routine that processes actions, such as keystrokes and mouse
movements.
It is the receipt of an event at some event handler from an event producer and subsequent
processes.
Event Handlers
Event Handler Description
onBlur It executes when the input focus leaves the field of a text, textarea or a select option.
onChange It executes when the input focus exits the field after the user modifies its text.
It executes when input focus enters the field by tabbing in or by clicking but not selecting
onFocus
input from the field.
onMouseOver The JavaScript code is called when the mouse is placed over a specific link or an object.
onMouseOut The JavaScript code is called when the mouse leaves a specific link or an object.
onReset It executes when the user resets a form by clicking on the reset button.
onSelect It executes when the user selects some of the text within a text or textarea field.
<html>
<head>
<script type="text/javascript">
function time()
{
var d = new Date();
var ty = d.getHours() + ":"+d.getMinutes()+":"+d.getSeconds();
document.frmty.timetxt.value=ty;
setInterval("time()",1000)
}
</script>
</head>
<body onload="time()">
<center><h2>Displaying Time</h2>
<form name="frmty">
<input type=text name=timetxt size="8">
</form>
</center>
</body>
</html>
Output:
<html>
<body>
<script>
function validateform()
{
var uname=document.myform.name.value;
var upassword=document.myform.password.value;
if (uname==null || uname=="")
{
alert("Name cannot be left blank");
return false;
}
else if(upassword.length<6)
{
alert("Password must be at least 6 characters long.");
return false;
}
}
function emailvalidation()
{
var a=document.myform.email.value
if (a.indexOf("@")==-1)
{
alert("Please enter valid email address")
document.myform.email.focus()
}
}
</script>
<body>
<form name="myform" method="post" action="validpage.html" onsubmit="return
validateform()">
Email: <input type="text" size="20" name="email" onblur="emailvalidation()"><br>
User Name: <input type="text" name="name"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Submit" >
</form>
</body>
</html>
<html>
<body>
<script type="text/javascript">
alert("You are a Valid User !!!");
</script>
</body>
</html>
Output:
Introduction to Arrays
Multidimensional Array
var arr2 = [
[1, 2, 3] ,
['a', 'b', 'c'] ,
['x', 'y', 'z']
];
function show_array(arr)
{
for (var i = 0; i < arr.length; i++ )
{
document.write(array[i]);
document.write('<br/>');
}
}
var arr1 = [1, 2, 3];
show_array(arr1);
Array Properties
Array Properties Description
Constructor It returns a reference to the array function that created the object.
Array Methods
Methods Description
concat() It returns a new array comprised of this array joined with other arrays and values.
every() It returns true if every element in this array satisfies the provided testing function.
It creates a new array with all the elements of this array for which the provided filtering
filter()
function returns true.
indexOf() It returns the first index of an element within the array equal to the specified value.
pop() It removes the last element from an array and returns that element.
push() It adds one or more elements to the end of an array and returns the new length of the array.
<html>
<body>
<button onclick="arrpop();">POP</button>
<button onclick="arrpush();"> PUSH </button>
<script>
function arrpop()
{
var numbers = ["1", "2", "3", "4", "5", "ABC"];
document.write(numbers.pop()+" "+"Removed"+"<br>");
//pop() removes the last element of an array
Output:
Built-in Objects
Built-in objects are not related to any Window or DOM object model.
These objects are used for simple data processing in the JavaScript.
1. Math Object
Math Properties
Math Property Description
PI Returns Π value.
Math Methods
Methods Description
<html>
<head>
<title>JavaScript Math Object Methods</title>
</head>
<body>
<script type="text/javascript">
Output
<html>
<head>
<title>JavaScript Math Object Properties</title>
</head>
<body>
<script type="text/javascript">
var value1 = Math.E
document.write("E Value is :" + value1 + "<br>");
E Value is :2.718281828459045
LN2 Value is :0.6931471805599453
LN10 Value is :2.302585092994046
PI Value is :3.141592653589793
2. Date Object
Syntax:
var variable_name = new Date();
Example:
var current_date = new Date();
Date Methods
Methods Description
getTime() Returns the number of milliseconds since January 1, 1970 at 12:00 AM.
getTimezoneOffset() Returns the timezone offset in minutes for the current locale.
setTime() Sets the number of milliseconds since January 1, 1970 at 12:00 AM.
<html>
<body>
<center>
<h2>Date Methods</h2>
<script type="text/javascript">
var d = new Date();
document.write("<b>Locale String:</b> " + d.toLocaleString()+"<br>");
document.write("<b>Hours:</b> " + d.getHours()+"<br>");
document.write("<b>Day:</b> " + d.getDay()+"<br>");
document.write("<b>Month:</b> " + d.getMonth()+"<br>");
document.write("<b>FullYear:</b> " + d.getFullYear()+"<br>");
document.write("<b>Minutes:</b> " + d.getMinutes()+"<br>");
</script>
</center>
</body>
</html>
Output:
3. String Object
Syntax:
var variable_name = new String(string);
Example:
var s = new String(string);
String Properties
Properties Description
constructor It returns the reference to the String function that created the object.
String Methods
Methods Description
charCodeAt() It returns the ASCII code of the character at the specified position.
concat() It combines the text of two strings and returns a new string.
split() It splits a string object into an array of strings by separating the string into the substrings.
<html>
<body>
<center>
<script type="text/javascript">
var str = "CareerRide Info";
var s = str.split();
document.write("<b>Char At:</b> " + str.charAt(1)+"<br>");
document.write("<b>CharCode At:</b> " + str.charCodeAt(2)+"<br>");
document.write("<b>Index of:</b> " + str.indexOf("ide")+"<br>");
document.write("<b>Lower Case:</b> " + str.toLowerCase()+"<br>");
document.write("<b>Upper Case:</b> " + str.toUpperCase()+"<br>");
</script>
<center>
</body>
</html>
Output:
Output:
Addition: 5
Introducing DOM
Document Object Model (DOM) is a standard object model and programming interface for
HTML.
It defines a standard for accessing documents.
It is a World Wide Consortium standard.
It is a platform and language-neutral interface that allows programs and scripts to dynamically
access and update the content, structure and style of a document.
1. DOM Object
DOM Properties
Properties Description
documentMode It returns the mode used by the browser to render the document.
Domain It returns the domain name of the server that loaded the document.
Referrer It returns the URL of the document that loaded the current document.
Methods Description
writeln() Same as write(), but adds a newline character after each statement.
<html>
<head>
<script type="text/javascript">
function check()
{
var username = document.getElementById("uname");
var password = document.getElementById("pass");
if(username.value=="abc" && password.value=="123")
alert("Hello!!! You are successfully signed in");
else
alert("Invalid Username and Password!!!");
}
</script>
</head>
<body>
<h2>Login</h2>
Username:<input type = "text" id="uname"><br>
Password:<input type = "password" id="pass"><br>
<input type="button" onClick="check()" Value="Sign In">
</body>
</html>
Output:
2. Element Object
Element object contains references of all elements in a form.
Property Description
<html>
<head>
<script type="text/javascript">
function welcome()
{
var fname = document.getElementById('fname');
var buttxt = document.getElementById('buttxt');
buttxt.innerHTML = fname.value;
}
</script>
</head>
<body>
<p>Welcome <b id = 'buttxt'></b></p>
Your Name : <input type = "text" id="fname"><br>
<input type = "button" onClick="welcome()" value="Enter">
</body>
</html>
Output:
3. Anchor Object
Property Description
1. Window Object
Property Description
Frames It returns an array of all the frames including iframes in the current window.
Closed It returns the boolean value indicating whether a window has been closed or not.
Method Description
prompt() It displays a dialog box that prompts the visitor for input.
<html>
<head>
<script type="text/javascript">
function openwindow()
{
window.open("welcome.html");
}
</script>
</head>
<body>
<form>
<input type="button" value="Open" onClick=window.alert()>
<input type="button" onClick="openwindow()" value="Open Window">
</form>
</body>
</html>
<html>
<body>
<script type="text/javascript">
{
document.write("<b>Welcome to TutorialRide !!!</b>");
}
</script>
</body>
</html>
Output:
In the above JavaScript program, when you click on the 'Open Window', you will see the
'welcome.html' opened in another window.
When you click on the 'Open' button, you will see the alert message box.
2. Navigator Object
Property Description
userAgent It returns the user agent header sent by the browser to the server.
Method Description
javaEnabled() It specifies whether or not the browser is Java enabled.
<html>
<body>
<script type="text/javascript">
document.write("<b>Browser: </b>"+navigator.appName+"<br><br>");
document.write("<b>Browser Version: </b>"+navigator.appVersion+"<br><br>");
document.write("<b>Browser Code: </b>"+navigator.appCodeName+"<br><br>");
document.write("<b>Platform: </b>"+navigator.platform+"<br><br>");
document.write("<b>Cookie Enabled: </b>"+navigator.cookieEnabled+"<br><br>");
document.write("<b>User Agent: </b>"+navigator.userAgent+"<br><br>");
document.write("<b>Java Enabled: </b>"+navigator.javaEnabled()+"<br><br>");
</script>
</body>
</html>
Output:
Browser: Netscape
Browser Version: 5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu
Chromium/37.0.2062.120 Chrome/37.0.2062.120 Safari/537.36
User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu
Chromium/37.0.2062.120 Chrome/37.0.2062.120 Safari/537.36
3. History Object
Property Description
Next It returns the URL of the next document in the History object.
Previous It returns the URL of the last document in the history object.
Method Description
4. Location Object
Property Description
port It returns the port number the server uses for a URL.
<html>
<body>
<script type="text/javascript">
document.write("<b>Path Name: </b>"+location.pathname+"<br><br>");
document.write("<b>Href: </b>"+location.href+"<br><br>");
document.write("<b>Protocol: </b>"+location.protcol+"<br><br>");
</script>
</body>
</html>
Output:
Href: file:///home/tutorialride2/location_object.html
Protocol: undefined
1. Form Object
Syntax:
<form> . . . </form>
Property Description
Action It sets and returns the value of the action attribute in a form.
enctype It sets and returns the value of the enctype attribute in a form.
Method It sets and returns the value of the method attribute in a form that is GET or POST.
Name It sets and returns the value of the name attribute in a form.
Target It sets and returns the value of the target attribute in a form.
Method Description
2. Hidden Object
Hidden object represents a hidden input field in an HTML form and it is invisible to the user.
This object can be placed anywhere on the web page.
It is used to send hidden form of data to a server.
Syntax:
<input type= “hidden”>
Property Description
Name It sets and returns the value of the name attribute of the hidden input field.
Value It sets or returns the value of the value attribute of the hidden input field.
3. Password Object
Syntax:
<input type= “password”>
maxLength It sets or returns the maximum number of characters allowed in a password filed.
Name It sets or returns the value of the name attribute of a password field.
Value It sets or returns the value of the attribute of the password field.
Method Description
4. Checkbox Object
Syntax:
<input type= “checkbox”>
Property Description
Method Description
Syntax:
<select> … </select>
Collection Description
Property Description
selectedIndex It sets or returns the index of the selected option in a dropdown list.
Method Description
6. Option Object
Syntax:
<option value> . . . </option>
Property Description
Value It sets or returns the value to the server if the option was selected.
Methods Description
<html>
<head>
<script type="text/javascript">
function optionfruit(select)
{
var a = select.selectedIndex;
var fav = select.options[a].value;
if(a==0)
{
alert("Please select a fruit");
}
else
{
document.write("Your Favorite Fruit is <b>"+fav+".</b>");
}
}
</script>
</head>
<body>
<form>
List of Fruits:
<select name="fruit">
<option value="0">Select a Fruit</option>
<option value="Mango">Mango</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Strawberry">Strawberry</option>
<option value="Orange">Orange</option>
</select>
<input type="button" value="Select" onClick="optionfruit(this.form.fruit);">
</form>
</body>
</html>
Output:
7. Radio Object
Radio object represents a radio button in an HTML form.
Syntax:
<input type= “radio”>
Property Description
Name It sets or returns the value of the name attribute of a radio button.
Method Description
8. Text Object
Text object represents a single-line text input field in an HTML form.
Syntax:
<input type= “text”>
Property Description
Name It sets or returns the value of the name attribute of a text field.
maxLength It sets or returns the maximum number of characters allowed in a text field.
9. Textarea Object
Textarea object represents a text-area in an HTML form.
Syntax:
<textarea> . . . </textarea>
Property Description
Name It sets or returns the value of the name attribute of a text field.