JavaScript With Forms
JavaScript With Forms
Engineering
Course Code: E2UC304C Course Name:Java & Java Script
1
Faculty Name: Dr. Avinash Dwivedi Programe Name: B.Tech (CSE,AI &ML)
Prerequisites, Objectives and Outcomes
Prerequisite of topic: Basic concepts related to programming
Objective: To make students to apply JavaScript in web programming.
Outcome : 1. Student will be able to know to use different ways to add
JavaScript in HTML.
2. Students will be able to apply DOM model in developing web pages.
3. Students will be able to implement in practical applications.
2
Javascript
Javascript is the only client side programming language for web browser.
JavaScript can build interactivity Websites.
Javascript is Object Based with prototype inheritance model for OOPS.
Javascript is Case Sensitive.
Javascript can put dynamic content into a webpage. .
Javascript can react to events like Mouse Click, mouse hover, mouse out, form
submit etc known as JavaScript Events.
Javascript can validate form data.
Javascript can detect user's browser and operating system using navigator Object.
Javascript can be used to create cookies.
Javascript can add cool animation to a webpage JS timing functions.
Javascript can detect user physical location using HTML5 Geolocation API.
Some popular facts about JavaScript.
Javascript can also be used to draw shapes, graphs, create animations and games
development using HTML5 Canvas.
At present, Javascript has lot of libraries and framework, exp JQuery, Angular JS,
React JS, Backbone JS etc, thus making Javascript more popular.
Javascript can also be used in developing server side application using Node JS.
Popular Editors like, VS Code, Atom and Brackets are written in Javascript (Electron
Framework of Node JS).
Advantages of scripts
Many browsers allow it to work with files, but the access is very limited and
only provided if the user is performing a specific action like, dropping a file
into a browser window or selecting using <input> tag.
JavaScript allows you to communicate over the net to the server where the
current page came from. Although, it does not allow you to receive data from
other sites/domains.
Three most important features of JS
Here, are the three most important features which
make JavaScript unique
</head>
<body style = "text-align: center;">
<h1 style = "color: red;">Hello World</h1>
<script>
</body></html>
External Scripts
var person = {
firstName: "James", lastName: "Bond", age: 25,
getFullName: function () {
return this.firstName + ' ' + this.lastName
}
};
person.getFullName();
Object Constructor
</html>
Confirm Box
<!DOCTYPE html>
<html><body>
<h2>JavaScript Confirm Box</h2>
<input type= “button” value= “Confirm Demo” onClick="myFunction()">
<p id="demo"></p>
<script>
function myFunction() {
var txt;
if (confirm("Press a button!")) {
txt = "You pressed OK!";
} else {
txt = "You pressed Cancel!";
}
document.getElementById("demo").innerHTML = txt;
}
Prompt Box
A prompt box is often used if you want the user to input a value
before entering a page.
When a prompt box pops up, the user will have to click either
"OK" or "Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user
clicks "Cancel" the box returns null.
Syntax
window.prompt("sometext","defaultText");
Program: Prompt Box
<!DOCTYPE html>
<html><head><title>JavaScript prompt() method</title>
<script>
function fun() {
var a = prompt("Enter some text", “Deafaul value GU");
if (a != null) {
document.getElementById("para").innerHTML = "Welcome to " + a;
}
}
</script>
</head>
<body style = "text-align: center;">
<h1 style = "color: red;">Hello World</h1><h2>
<button onclick = "fun()">Click me</button>
<p id = "para"></p>
</body></html>
Adding 2 Numbers using Prompt Box
<!DOCTYPE html>
<html><head><title>JavaScript prompt() method</title>
<script>
function fun() {
var num1 = parseInt(window.prompt("Enter the first number"));
var num2 = parseInt(window.prompt("Enter the second number "));
alert(num1+num2)
}
</script>
</head>
<body style = "text-align: center;">
<h1 style = "color: red;">Hello World</h1>
<button onclick = "fun()">Click me</button>
</body></html>
window object
The window object represents a window in browser. An object of window is
created automatically by the browser.
Window is the object of browser, it is not the object of javascript. The
javascript objects are string, array, date etc.
Methods of Window object
Method Description
alert() displays the alert box containing message with ok button.
confirm() displays the confirm dialog box containing message with ok and
cancel button.
The JavaScript navigator object is used for browser detection. It can be used
to get browser information such as appName, appCodeName, userAgent etc.
window.navigator
Property of JavaScript navigator object
10 mimeTypes[] returns the array of mime type. It is supported in Netscape and Firefox only.
• Environment objects
– Window, Navigator, Screen, History, Location, Document
• HTML objects
– Anchor, Area, Base, Body, Button, Event, Form, Frame,
Frameset, Iframe, Image, Checkbox, FileUpload, Hidden,
Password, Radio, Reset, Submit, Text, Link, Meta, Object,
Option, Select, Style, Table, TableCell, TableRow, TextArea
HTML DOM: Document
79
Thank you
80