Unit3 Part1 Notes Javascript_ajax
Unit3 Part1 Notes Javascript_ajax
JavaScript Introduction:
JavaScript is the Scripting Language for the Web.
JavaScript can update and change both HTML and CSS.
JavaScript can calculate, manipulate and validate data.
Uses of Javascript:
Validation: JavaScript can be used to validate HTML forms, such
as checking if required fields are filled out, or if a date is
valid. JavaScript can validate many different fields, including
passwords, email addresses, and mobile numbers.
Calculation: JavaScript can be used for calculations. For
example:Program to add two numbers in javascript and html
Animation: JavaScript can be used for animation.For
example:aumatically changing images after sometime in
website.
Some other uses of javascript
Advantages of Javascript
Simplicity ‒ having a simple structure makes JavaScript easier
to learn and implement, and it also runs faster than some
other languages.
Speed ‒ JavaScript executes scripts directly within the web
browser without connecting to a server first or needing a
compiler. Additionally, most major browsers allow JavaScript to
compile code during program execution.
Versatility ‒ JavaScript is compatible with other languages like
PHP, Perl, and Java. It also makes data science and machine
learning accessible to developers.
Server load ‒ another perk of operating on the client-side is that
JavaScript reduces the requests sent to the server.
Method Description
Document Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p id="intro">Finding HTML Elements by Id</p>
<p>This example demonstrates the getElementsById
method.</p>
<p id="demo"></p>
<script language =”javascript”>
const element = document.getElementById("intro");
document.getElementById("demo").innerHTML =
"The text from the intro paragraph is: " + element.innerHTML;
</script>
</body>
</html>
Output:
JavaScript HTML DOM
Finding HTML Elements by Id
This example demonstrates the getElementsById method.
The text from the intro paragraph is: Finding HTML Elements by
Id
Forms:
An HTML form is used to collect user input. The user input is
most often sent to a server for processing.
The <form> Element
The HTML <form> element is used to create an HTML form for
user input:
The <input> Element
The HTML <input> element is the most used form element.
An <input> element can be displayed in many ways, depending
on the type attribute.
Here are some examples:
Type Description
Output:
Alert Box
Syntax
alert("sometext");
Confirm Box
A confirm box is often used if you want the user to verify or accept
something.
When a confirm box pops up, the user will have to click either "OK"
or "Cancel" to proceed. If the user clicks "OK", the box returns true.
If the user clicks "Cancel", the box returns false.
Syntax
confirm("sometext");
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
JavaScript Statements
document.write("Hello Dolly");
JavaScript Comments
Javascript variables :
Note
The var keyword was used in all JavaScript code from 1995 to
2015.
The let and const keywords were added to JavaScript in 2015.
The var keyword should only be used in code written for older
browsers.
Var example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"> </p>
<script language=”javascript”>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>
</body>
</html>
Output:
JavaScript Variables
In this example, x, y, and z are variables.
The value of z is: 11
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"></p>
<script language=”javascript”>
let x = 5;
let y = 8;
let z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>
</body>
</html>
Output:
JavaScript Variables
In this example, x, y, and z are variables.
The value of z is: 13
Functions in Javascript:
A JavaScript function is a block of code designed to perform a
particular task.
Simple alert statements using function and how function call works in
java:
<html>
<body>
<script language=”javascript”>
function fun1() {
alert("functions called");
}
</script>
<h1>JavaScript Functions</h1>
<p>Call a function which performs a calculation and returns the
result:</p>
</body>
</html>
Program to input and add two numbers in javascript using
function call alert and document:
<html>
<head>
<script language=”javascript”>
function add()
{
var num1, num2, sum;
num1 =
parseInt(document.getElementById("firstnumber").value);
num2 =
parseInt(document.getElementById("secondnumber").value);
sum=num1+num2;
alert("sum is"+sum);
document.getElementById("answer").value = sum;
}
</script>
</head>
<p>First Number: <input typr="text" id="firstnumber"></p>
<p>Second Number: <input type="text"
id="secondnumber"></p>
<button onclick="add()">Add Them</button>
<p>Sum = <input id="answer"></p>
</body>
</html>
Java is an object-oriented
JavaScript is a scripting
programming language
language used for creating
primarily used for developing
interactive and dynamic web
complex enterprise
pages.
applications.
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Arithmatic opeartor
Operato Description
r
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
++ Increment
-- Decrement
Comparison Operators:
Operato Description
r
== equal to
!= not equal
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<p id="demo"></p>
<script language=”javascript”>
const cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
</script>
</body>
</html>
Output:
JavaScript Arrays
Saab,Volvo,BMW
Loops in javascript:
</script>
<body>
<h1>Generate Table</h1>
Enter any number<input type="text" id="num">
<input type="text" id="a">
<input type="submit" onclick="table()">
</body>
</html>
Output:
Enter any number 5
5 10 15 20 25 30 35
Conditional Statements
Very often when you write code, you want to perform different
actions for different decisions.
Event Description
Onmouseout The user moves the mouse away from an HTML element
XMLHTTPRequest object:
ajax_info.htm
<h1>AJAX</h1>
<p>AJAX is not a programming language.</p>
<p>AJAX is a technique for accessing web servers from a web
page.</p>
<p>AJAX stands for Asynchronous JavaScript And XML.</p>