Working With Javascript:: Unit - 5
Working With Javascript:: Unit - 5
+ UNIT -5
OBJECT—EVENTS,EVENTS HANDLERS,FORMS—
VALIDATIONS,FORM ENHANCEMENTS,JAVASCRIPT
LAIBRARIES.
PRACTICAL TIPS FOR WRITING JAVASCRIPT:
JAVASCRIPT OBJECTS:
Example:
let w = window.innerWidth;
let h = window.innerHeight;
PROGRAM:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Window</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
</script>
</body>
</html>
OUTPUT:
JavaScript Window
Screen object:
The window.screen object contains information about the user's
screen.
Window Screen:
Properties:
• screen.width
• screen.height
• screen.availWidth
• screen.availHeight
• screen.colorDepth
• screen.pixelDepth
Example
Example
document.getElementById("demo").innerHTML =
"Screen Height: " + screen.height;
Example
document.getElementById("demo").innerHTML =
"Available Screen Width: " + screen.availWidth;
Example:
document.getElementById("demo").innerHTML =
"Available Screen Height: " + screen.availHeight;
Very old computers, and old cell phones used 8 bits: 256
different "VGA colors".
Example
document.getElementById("demo").innerHTML =
"Screen Color Depth: " + screen.colorDepth;
Example
document.getElementById("demo").innerHTML =
"Screen Pixel Depth: " + screen.pixelDepth;
Result will be:
Document Object:
The HTML DOM document object is the owner of all other objects
in your web page.
Below are some examples of how you can use the document
object to access and manipulate HTML.
Method Description
Property Description
Method Description
Form Object:
Example
var x = document.getElementById("myForm");
Tip: You can also access a <form> element by using
the forms collection.
Example:
var x = document.createElement("FORM");
Collection Description
Property Description
Method Description
1. window.navigator
Or,
1. navigator
<script>
document.writeln("<br/>navigator.appCodeName:
"+navigator.appCodeName);
document.writeln("<br/>navigator.appName:
"+navigator.appName);
document.writeln("<br/>navigator.appVersion:
"+navigator.appVersion);
document.writeln("<br/>navigator.cookieEnabled:
"+navigator.cookieEnabled);
document.writeln("<br/>navigator.language:
"+navigator.language);
document.writeln("<br/>navigator.userAgent:
"+navigator.userAgent);
document.writeln("<br/>navigator.platform:
"+navigator.platform);
document.writeln("<br/>navigator.onLine: "+navigator.onLine);
</script>
hidden None
reset onReset.
submit onSubmit
• submit
• reset
• button
For forms with multiple input fields, it get easier to refer them
using this keyword, than by using full name to call the event
handler function.
PROGRAM1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
function highlightInput(elm){
elm.style.background = "lightgreen";
</script>
<button type="button">Button</button>
</body>
</html>
PROGRAM2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
PROGRAM 3:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<option>Select</option>
<option>OnePlus</option>
<option>Samsung</option>
</select>
</body>
</html>
OUTPUT:
OnePlus
PROGRAM4:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<label>First Name:</label>
</form>
</body>
</html>
OUTPUT:
Submit
First Name:
FORM ENHANCEMENTS
• Validating forms
• Manipulating cookies
JavaScript Example
function validateForm() {
let x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
let x = document.forms["myForm"]["fname"].value;
if (x == "") {
return false;
</script>
</head>
<body>
<h2>JavaScript Validation</h2>
</form>
</body>
</html>
OUTPUT:
JavaScript Validation
Submit
Name:
Submit
PROGRAM:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Validation</h2>
<p>Please input a number between 1 and 10:</p>
<input id="numb">
<button type="button" onclick="myFunction()">Submit</button>
<p id="demo"></p>
<script>
function myFunction() {
// Get the value of the input field with id="numb"
let x = document.getElementById("numb").value;
// If x is Not a Number or less than one or greater than 10
let text;
if (isNaN(x) || x < 1 || x > 10) {
text = "Input not valid";
} else {
text = "Input OK";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>
OUTPUT:
JavaScript Validation
Please input a number between 1 and 10:
Submit
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Validation</h2>
</form>
<p>If you click submit, without filling out the text field,
</body>
</html>
OUTPUT:
JavaScript Validation
Submit
If you click submit, without filling out the text field, your browser will display an
error message.
JavaScript Libraries
<scriptsrc="//cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js"><
/script>
<scriptsrc="js/lib/jquery.min.js"></script>
Common libraries
Ember is a JavaScript
EmberJS Framework framework for creating
ambitious web applications that
Library Type Description
Ext JS is a JavaScript
framework for creating business
application interfaces, it
provides an MVC-style
application architecture, a set of
Ext JS Framework standard UI widgets such as
grids and trees, a theming and
templating system, a charting
and drawing library and other
utilities for creating rich data-
oriented applications.
Prototype is a JavaScript
Framework that aims to ease
development of dynamic web
applications. Featuring a
unique, easy-to-use tool kit for
Framework
Prototype class-driven development and
& Utilities
the nicest Ajax library around,
Prototype is quickly becoming
the code base of choice for web
application developers
everywhere.
SproutCore is an open-source
SproutCore Framework JavaScript framework. Its goal
is to allow developers to create
Library Type Description
Underscore is a utility-belt
library for JavaScript that
provides a lot of the functional
programming support that you
would expect in Prototype.js (or
Underscore Framework
Ruby), but without extending
any of the built-in JavaScript
objects. It’s the tie to go along
with jQuery’s tux, and
Backbone.js’s suspenders.