JAVASCRIPT_EVENTS
JAVASCRIPT_EVENTS
<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:
JAVA SCRIPT EVENTS : Live Demo
<html>
<head>
<!--
function sayHello() {
alert("Hello World")
//-->
</script>
</head>
<body>
<form>
</form>
</body>
</html>
Output
Example
The following example shows how to use onsubmit. Here we are calling a validate() function before
submitting a form data to the webserver. If validate() function returns true, the form will be submitted,
otherwise it will not submit the data.
Try the following example.
<html>
<head>
<!--
function validation() {
.........
//-->
</script>
</head>
<body>
.......
</form>
</body>
</html>
<html>
<head>
<!--
function over() {
function out() {
//-->
</script>
</head>
<body>
</div>
</body>
</html>
Output
HTML 5 Standard Events
Event Handlers
Event Description
Handler
onAbort It executes when the user aborts loading an image.
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.
onFocus It executes when input focus enters the field by tabbing in or by clicking but not
selecting 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.
Output:
<html>
<body>
<script type="text/javascript">
alert("You are a Valid User !!!");
</script>
</body>
</html>
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
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>");
Output:
E Value is :2.718281828459045
LN2 Value is :0.6931471805599453
LN10 Value is :2.302585092994046
PI Value is :3.141592653589793
2. Date Object
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
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 substr
<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:
Introducing DOM
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.
DOM Methods
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
Anchor object represents an HTML hyperlink.
It allows you to create a link to another document with the 'href' attribute.
You can access an anchor by using getElementById() or by searching through
the anchors[ ] array of the Document object.
You can add anchors to the anchors[ ] array but you cannot delete, replace or
modify them.
Anchor Object Properties
Property Description
<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.