UNIT-IV DHTML With Javascript
UNIT-IV DHTML With Javascript
Data Validation
Validation is simply the process of ensuring that some data might be correct data
for a particular application. Data validation is the process of ensuring that users
A common technique for restricting access to a site is to check user IDs against a
The Regular expression which checks that any name entered by users only has
If a program accepts the data from a remote data logger and that input is always going to be in
a particular range, then the program knows that data outside the range is valid and should not
be accepted.
Example: 1
The following example has two text fields. One accepts names, the other accepts age. Both
fields are validated using regular expressions and if the date is valid the contents of the form
The below function checks if a field has been left empty. If the field is blank, an alert box
alerts a message, the function returns false, and the form will not be submitted:
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
return false;
</script>
</head>
<body>
method="post">
</form>
</body>
</html>
Output:
The function below checks if the content has the general syntax of an email. This means
that the input data must contain a @ sign and at least one dot (.). Also, the @ must not be the
first character of the email address, and the last dot must be present after the @ sign, and
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp" onsubmit="return
validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>
</html>
Output:
******************************************************************************
frames (<frame> or <iframe> tags), the browser creates one window object for the HTML
The open() method opens a new browser window. The general format is
window.open(URL,name,specs,replace);
******************************************************************************
The status property sets or returns the text in the status bar at the bottom of the
Example:
<!DOCTYPE html>
<html>
<body>
<script>
</script>
<p>Note: This property does not work in default configuration of IE, Firefox,
</body>
</html>
Output:
******************************************************************************
Definition
HTML Frame used to split the browser window in multiple individual frames that can contain
HTML document within frame include a other web pages link can be opened in the desired
frame.
Advantages of Frames
It facility to reduce downloading time and improves the usability of the website.
Frames generally include navigation link, header or footers, which help user to find and
It separates content of website from navigation elements, which is useful for website
Disadvantages of Frames
The web developer must be track of more HTML documents linked with main frame.
pixels *: Allocated remaining size of the window. Eg. Create two vertical
the window.
Frames.html
<html>
<frameset cols="15%,85%">
<frame src="frame_a.html" name="list">
<frame src="frame_b.html" name="output">
</frameset>
</html>
Frame_a.html
<html>
<head>
<title>Frames</title>
</head>
<body bgcolor="#ccfff5">
<h3 align="center">This Is Frame A</h3>
<a href="program1.html" target="output">Program1</href><br>
<a href="program2.html" target="output">Program2</href><br>
<a href="program3.html" target="output">Program3</href>
</body>
</html>
Frame_b.html
<html>
<head>
<title>frame_b</title>
</head>
<body bgcolor="#ffcc99">
<h1 align="center">This Is Frame B</h1>
</body>
</html>
Program1.html
<html>
<head>
<title>Program1</title>
</head>
<body bgcolor="#99e6ff">
<h1 align="center">This is Program1</h1>
</body>
</html>
Program2.html
<html>
<head>
<title>Program2</title>
</head>
<body bgcolor="#ff80aa">
<h1 align="center">This is Program2</h1>
</body>
</html>
Program3.html
<html>
<head>
<title>Program3</title>
</head>
<body bgcolor="#ffffcc">
<h1 align="center">This is Program3</h1>
</body>
</html>
Output:
******************************************************************************
which the appearance of a graphical image changes when the user rolls the mouse pointer
over it.
Rollover also refers to a button on a Web page that allows interactivity between the
user and the Web page. It causes the button to react by either replacing the source image
Rollover is triggered when the mouse moves over the primary image, causing the
secondary image to appear. The primary image reappears when the mouse is moved away.
On many web pages, javascript rollovers are handled by adding an onmouseover and
2. onmouseout is triggered when the mouse moves away from the element
Makes a hidden image or element to appear when the mouse is moved over it
Makes an element of the page change the color of the entire Web page when the mouse is
moved over it
Causes text to pop up or become highlighted with bold colors when the mouse is moved on
a text element.
Example:
<html>
<body>
<center>
<h1 onmouseover="this.style.color='red'" onmouseout="this.style.color='green'">
JavaScript RollOver</h1>
<img src="C:/Pictures/myhand.jpg" onmouseover="this.src='C:/Pictures/cars.jpg'"
onmouseout="this.src='C:/Pictures/cars2.jpg'" height=200 width=300 border=1>
</center>
</body>
</html>
Output:
******************************************************************************
JavaScript can be used to move a number of DOM elements (<img />, <div> or any
other HTML element) around the page according to some sort of pattern determined by a
programs.
setInterval(function, duration)
clearTimeout(setTimeout_variable)
This function calls clears any timer set by the setTimeout() functions.
JavaScript can also set a number of attributes of a DOM object including its position on
the screen. We can set top and left attribute of an object to position it anywhere on the
OR
Example:
<html>
<head>
<title>JavaScript Animation</title>
<script type="text/javascript">
function init(){
imgObj = document.getElementById('myImage');
imgObj.style.position= 'relative';
imgObj.style.left = '0px';
}
function moveRight(){
imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';
animate = setTimeout(moveRight,200); // call moveRight in 200msec
}
function stop(){
clearTimeout(animate);
imgObj.style.left = '0px';
}
window.onload =init;
</script>
</head>
<body>
<form>
<img id="myImage" src="/images/html.gif" />
<p>Click the buttons below to handle animation</p>
<input type="button" value="Start" onclick="moveRight();" />
<input type="button" value="Stop" onclick="stop();" />
</form>
</body>
Prepared By: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science
Page No:-105
</html>
Output:
******************************************************************************
An event is defined as something that takes place and that is exactly what it means
in web programming as well. An event handler is JavaScript code that is designed to run
Example:
Categories of events
b) Mouse events
c) Key events
d) HTML events
User interface events happen as controls or other objects on a web page gain and lose
focus. These events are often caused by other user actions such as a tab key press. They
Mouse events occur when the user moves the mouse or presses one of the mouse buttons.
Key events occur when the user presses and/or releases one of the keyboard keys. Only
Finally, there are several events specific to certain HTML elements. They often relate
to the browser window itself or to form controls and other objects embedded in a web page.
User interface events deal exclusively with the transfer of focus from one object inside
the web page to another. There are three user interface events defined in most web
browsers.
JavaScript programs have the ability to track mouse movement and button clicks as
Like the user interface events, key events fire in a predictable sequence. There are
An HTML event means any events that do not belong in the user interface, mouse, or
key event categories. Some HTML events are triggered directly by a user action, while others
<html>
<head>
<title>Demo on Key events</title>
</head>
<script language="javascript">
function fun()
{
Prepared By: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science
Page No:-108
<body>
<form>
Key Press: <input type="text" onKeyPress="fun()"><br>
Key Down: <input type="text" onKeyDown="fun1()"><br>
Key Up: <input type="text" onKeyUp="fun2()"><br>
</form>
</body>
</html>
Output: