Unit IV Final
Unit IV Final
Introduction to JavaScript
JavaScript is the most popular scripting language on the Internet, and works in all
major browsers, such asInternet Explorer, Mozilla Firefox, and Opera.
What is JavaScript?
Java and JavaScript are two completely different languages in both concept and
design!
ID #demo getElementById()
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
html { font-family: sans-serif; color: #333; }
body { max-width: 500px; margin: 0 auto; padding: 0 15px; }
div, article { padding: 10px; margin: 5px; border: 1px solid #dedede; }
</style>
</head>
<body>
<h2>ID (#demo)</h2>
<div id="demo">Access me by ID</div>
<h2>Class (.demo)</h2>
<div class="demo">Access me by class (1)</div>
<div class="demo">Access me by class (2)</div>
<h2>Tag (article)</h2>
<article>Access me by tag (1)</article>
<article>Access me by tag (2)</article>
<h2>Query Selector</h2>
<div id="demo-query">Access me by query</div>
EXAMPLE
<html>
<body>
</html>
Output:
What is an Event?
Event Handlers
Directly or indirectly, an Event is always the result of something a user does. For
example, we've alreadyseen Event Handlers like onClick and onMouseOver
that respond to mouse actions. Another type of Event, an internal change-of-state
to the page (completion of loading or leaving the page). An onLoad Event can
be considered an indirect result of a user action.
The elements on a page which can trigger events are known as "targets" or
"target elements," and we can easily understand how a button which triggers a
Click event is a target element for this event. Typically, events are defined
through the use of Event Handlers, which are bits of script that tell the browser
what todo when a particular event occurs at a particular target. These Event
Handlers are commonly written as attributes of the target element's HTML tag.
The Event Handler for a Click event at a form field button element is quite simple to
understand:
There are "three different ways" that Event Handlers can be used to trigger Events
or Functions.
<A HREF="foo.html"
onMouseOver="doSomething()"> ... </A>
<A HREF="foo.html"
onMouseOver="alert('Hello!');">Mouse
over me!
</A>
click)
The second technique we've seen for triggering a Function in response to a mouse
action is to place an
onClick Event Handler inside a button type form element, like this:
<FORM>
<INPUT TYPE="button" onClick="alert('Hello!');">
</FORM>
While any JavaScript statement, methods, or functions can appear inside the
quotation marks of an EventHandler, typically, the JavaScript script that makes
up the Event Handler is actually a call to a function defined in the header of the
document or a single JavaScript command. Essentially, though, anything that
appears inside a command block (inside curly braces {}) can appear between the
quotation marks.
in text)
For instance, if you have a form with a text field and want to call the function
checkField() whenever thevalue of the text field changes, you can define your text
field as follows:
Nonetheless, the entire code for the function could appear in quotation marks rather
than a function call:
The third technique is to us an Event Handler to ensure that all required objects are
defined involve the onLoad and onUnLoad. These Event Handlers are defined in
the <BODY> or <FRAMESET> tag of anHTML file and are invoked when the
document or frameset are fully loaded or unloaded. If you set a flag
within the onLoad Event Handler, other Event Handlers can test this flags to see
if they can safely run,with the knowledge that the document is fully loaded and
all objects are defined. For example:
<SCRIPT>
var loaded = false;
function doit()
{
// alert("Everything is \"loaded\" and loaded = " + loaded);
alert('Everything is "loaded" and loaded = ' + loaded);
}
</SCRIPT>
<BODY onLoad="loaded = true;">
-- OR --
<BODY onLoad="window.loaded = true;">
<FORM>
<INPUT TYPE="button" VALUE="Press Me"
onClick="if (loaded == true) doit();">
-- OR --
<INPUT TYPE="button" VALUE="Press Me"
onClick="if (window.loaded == true) doit();">
-- OR --
<INPUT TYPE="button" VALUE="Press Me"
onClick="if (loaded) doit();">
</FORM>
</BODY>
The onLoad Event Handler is executed when the document or frameset is fully
loaded, which means that all images have been downloaded and displayed, all
subframes have loaded, any Java Applets and Plugins(Navigator) have started
running, and so on. The onUnLoad Event Handler is executed just before the
page is unloaded, which occurs when the browser is about to move on to a new
page. Be aware that whenyou are working with multiple frames, there is no
guarantee of the order in which the onLoad Event Handler is invoked for the
various frames, except that the Event Handlers for the parent frame is invoked
after the Event Handlers of all its children frames -- This will be discussed in
detail in Week 8.
The first example allows the user to change the color by clicking buttons,
while the second exampleallows you to change colors by using drop down
boxes.
Event
Handlers
EVENT DESCRI
PTION
onAbort the user cancels loading of an image
input focus is removed from a form element (when the user clicks outside the
onBlur
field) orfocus is removed from a window
onClick the user clicks on a link or form element
onChange the value of a form field is changed by the user
onError an error happens during loading of a document or image
onFocus input focus is given to a form element or a window
onLoad once a page is loaded, NOT while loading
onMouseOut the user moves the pointer off of a link or clickable area of an image map
onMouseOver the user moves the pointer over a hypertext link
onReset the user clears a form using the Reset button
onSelect the user selects a form element’s field
onSubmit a form is submitted (ie, when the users clicks on a submit button)
onUnload the user leaves a page
Note: Input focus refers to the act of clicking on or in a form element or field.
This can be done byclicking in a text field or by tabbing between text fields.
Which Event
Handlers
Can Be Used
Return Value: It returns a string which represents the position type of the element.
Properties:
absolute: It positions the element relative to the first positioned element.
Example:
<html>
<head>
<style>
#myDIV {
border: 1px solid black;
background-color: lightblue;
position: relative;
top: 20px;
}
</style>
</head>
<body>
<p>Click the "Try it" button to change the position property of the DIV element:</p>
<div id="myDIV">
<p>This DIV element is placed 20 pixels from the top of its original position.</p>
<script>
function myFunction() {
document.getElementById("myDIV").style.position = "absolute";
}
</script>
</body>
</html>
Output:
Example:
<html>
<head>
<style>
#myDIV {
border: 1px solid black;
background-color: lightblue;
position: relative;
top: 20px;
}
</style>
</head>
<body>
<p>Click the "Try it" button to change the position property of the DIV element:</p>
<div id="myDIV">
<p>This DIV element is placed 20 pixels from the top of its original position.</p>
<script>
function myFunction() {
document.getElementById("myDIV").style.position = "static";
}
</script>
</body>
</html>
<html>
<head>
<style>
#myDIV {
border: 1px solid black;
background-color: lightblue;
position: relative;
top: 20px;
}
</style>
</head>
<body>
<p>Click the "Try it" button to change the position property of the DIV element:</p>
<div id="myDIV">
<p>This DIV element is placed 20 pixels from the top of its original position.</p>
<script>
function myFunction() {
document.getElementById("myDIV").style.position = "fixed";
}
</script>
</body>
</html>
relative: It positions the elements relative to the normal position, so “right:40”
signifies an addition of 40 pixels to the element’s RIGHT position.
Example:
<html>
<head>
<style>
#myDIV {
border: 1px solid black;
background-color: lightblue;
position: relative;
top: 20px;
}
</style>
</head>
<body>
<p>Click the "Try it" button to change the position property of the DIV element:</p>
<div id="myDIV">
<p>This DIV element is placed 20 pixels from the top of its original position.</p>
<script>
function myFunction() {
document.getElementById("myDIV").style.position = "relative";
}
</script>
</body>
</html>
sticky: It positions the elements based on the scroll position of the user.The scrolling
operation is performed between relative and fixed property values. By default, the
scroll position is set at relative value.
Example:
<html>
<head>
<style>
#myDIV {
border: 1px solid black;
background-color: lightblue;
position: relative;
top: 20px;
}
</style>
</head>
<body>
<p>Click the "Try it" button to change the position property of the DIV element:</p>
<div id="myDIV">
<p>This DIV element is placed 20 pixels from the top of its original position.</p>
<script>
function myFunction() {
document.getElementById("myDIV").style.position = "sticky";
}
</script>
</body>
</html>
<p>Click the "Try it" button to change the position property of the DIV element:</p>
<div id="myDIV">
<p>This DIV element is placed 20 pixels from the top of its original position.</p>
<script>
function myFunction() {
document.getElementById("myDIV").style.position = "initial";
}
</script>
</body>
</html>
<html>
<head>
<style>
#myDIV {
border: 1px solid black;
background-color: lightblue;
position: relative;
top: 20px;
}
</style>
</head>
<body>
<p>Click the "Try it" button to change the position property of the DIV element:</p>
<div id="myDIV">
<p>This DIV element is placed 20 pixels from the top of its original position.</p>
<script>
function myFunction() {
document.getElementById("myDIV").style.position = "inherit";
}
</script>
</body>
</html>
object.style.visibility =
"visible|hidden|collapse|initial|inherit"
Property Values
Value Description
collapse When used on a table row or cell, the element is not visible
(same as "hidden")
<script>
function demoDisplay() {
document.getElementById("myP1").style.display = "visible";
}
function demoVisibility() {
document.getElementById("myP2").style.visibility = "hidden";
}
</script>
</body>
</html>
OUTPUT:
Ajax Introduction:
What is Ajax?
AJAX = Asynchronous JavaScript and XML.
AJAX is a technique for creating fast and dynamic web pages.
AJAX allows web pages to be updated asynchronously by exchanging small amounts
of data with the server behind the scenes. This means that it is possible to update parts
of a web page, without reloading the whole page.
Classic web pages, (which do not use AJAX) must reload the entire page if the
content should change.
Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook
tabs.