Java Scripts - Merged
Java Scripts - Merged
What is JavaScript
Although, JavaScript has no connectivity with Java programming language. The name was
suggested and provided in the times when Java was gaining popularity in the market. In
addition to web browsers, databases such as CouchDB and MongoDB uses JavaScript as their
scripting and query language.
Features of JavaScript
There are following features of JavaScript:
1. All popular web browsers support JavaScript as they provide built-in execution environments.
2. JavaScript follows the syntax and structure of the C programming language. Thus, it is a structured
programming language.
3. JavaScript is an object-oriented programming language that uses prototypes rather than using classes
for inheritance.
4. It is a light-weighted and interpreted language.
5. It is a case-sensitive language.
6. JavaScript is supportable in several operating systems including, Windows, macOS, etc.
7. It provides good control to the users over the web browsers.
Client-side scripting
Client-side scripting is when the server sends the code along with the HTML web page to the client.
The script is referred to by the code.
In other words, client-side scripting is a method for browsers to run scripts without having to connect
to a server.
The code runs on the client’s computer’s browser either while the web page is loading or after it has
finished loading.
Client-side scripting is mostly used for dynamic user interface components including pull-down menus,
navigation tools, animation buttons, and data validation.
It is currently quickly expanding and evolving on a daily basis. As a result, creating client-side web
programming has become easier and faster, lowering server demand.
By far the most popular client-side scripting languages or web scripting languages, JavaScript and
jQuery are frequently utilized to construct dynamic and responsive webpages and websites.
The browser downloads the code to the local machine (temporarily) and begins processing it without
the server. As a result, client-side scripting is browser-specific.
Simple java script example
The example above "finds" an HTML element (with id="demo"), and changes the element content (innerHTML) to
"Hello JavaScript":
JavaScript Variable
A JavaScript variable is simply a name of storage location. There are two types of variables in JavaScript :
1. local variable
2. global variable.
There are some rules while declaring a JavaScript variable (also known as identifiers).
1. Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
2. After first letter we can use digits (0 to 9), for example value1.
3. JavaScript variables are case sensitive, for example x and X are different variables.
1. <script>
2. function abc(){
3. var x=10;//local variable
4. }
5. </script>
A JavaScript global variable is accessible from any function. A variable i.e. declared outside the function or declared with
window object is known as global variable. For example:
1. <script>
2. var value=50;//global variable
3. function a(){
4. alert(value);
5. }
6. function b(){
7. alert(value);
8. }
9. </script>
JavaScript Functions
A JavaScript function is a block of code designed to perform a particular task.
Syntax
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
Function Return
When JavaScript reaches a return statement, the function will stop executing.
If the function was invoked from a statement, JavaScript will "return" to execute the code after the
invoking statement.
Functions often compute a return value. The return value is "returned" back to the "caller":
Example
function myFunction(a, b) {
// Function returns the product of a and b
return a * b;
}
Conditional Statements
Conditional statements are used to perform different actions based on different conditions.
The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate a JavaScript error.
Example
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening":
The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Example
If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00, create a
"Good day" greeting, otherwise a "Good evening":
The JavaScript Switch Statement
Use the switch statement to select one of many code blocks to be executed.
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example
This example uses the weekday number to calculate the weekday name:
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while, do while or for-in loops. It
makes the code compact. It is mostly used in array.
Loops can execute a block of code a number of times.
Expression 1 is executed (one time) before the execution of the code block.
Expression 3 is executed (every time) after the code block has been executed.
Syntax
for (key in object) {
// code block to be executed
}
Example
Syntax
while (condition) {
// code block to be executed
}
Example
In the following example, the code in the loop will run, over and over again, as long as a variable (i) is
less than 10:
Syntax
do {
// code block to be executed
}
while (condition);
Example
The example below uses a do while loop. The loop will always be executed at least once, even if the
condition is false, because the code block is executed before the condition is tested:
JavaScript Objects
A javaScript object is an entity having state and behavior (properties and method). For example: car, pen,
bike, chair, glass, keyboard, monitor etc.
JavaScript is an object-based language. Everything is an object in JavaScript.
JavaScript is template based not class based. Here, we don't create class to get the object. But, we direct
create objects.
1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)
object={property1:value1,property2:value2.....propertyN:valueN}
When a web page is loaded, the browser creates a Document Object Model of the page. The
• Document object − Each HTML document that gets loaded into a window
becomes a document object. The document contains the contents of the
page.
• Form control elements − The form object contains all the elements
defined for that object such as text fields, buttons, radio buttons, and
checkboxes.
The HTML DOM model is constructed as a tree of Objects. So it is a tree structure.
With the object model, JavaScript gets all the power it needs to create dynamic
HTML: It can perform the following operations :
DOM Definition
Document Object
Is same as
1. document
Method Description
getElementsByName() returns all the elements having the given name value.
getElementsByTagName() returns all the elements having the given tag name.
getElementsByClassName() returns all the elements having the given class name.
Components
1. HTML DOM
The HTML DOM is a standard object model and programming interface for HTML. It
defines:
Types of DOM
• The Legacy DOM − This is the model which was introduced in early
versions of JavaScript language. It is well supported by all browsers, but
allows access only to certain key portions of documents, such as forms,
form elements, and images.
• The W3C DOM − This document object model allows access and
modification of all document content and is standardized by the World
Wide Web Consortium (W3C). This model is supported by almost all the
modern browsers.
• The IE4 DOM − This document object model was introduced in Version
4 of Microsoft's Internet Explorer browser. IE 5 and later versions include
support for most basic W3C DOM features
It is important to validate the form submitted by the user because it may contain
inappropriate values. So, validation is must to authenticate user.
Importance
JavaScript provides facility to validate the form on the client-side so data processing
will be faster than server-side validation. Most of the web developers prefer JavaScript
form validation.
Through JavaScript, we can validate name, password, email, date, mobile numbers
and more fields.
Example
In this example, we are going to validate the name and password. The name can’t be
empty and password can’t be less than 6 characters long.
Here, we are validating the form on form submit. The user will not be forwarded to the
next page until given values are correct.
<script>
function validateform(){
var name=document.myform.name.value;
var password=document.myform.password.value;
if (name==null || name==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
<body>
<form name="myform" method="post" action="abc.jsp" onsubmit="return validatefor
m()" >
Name: <input type="text" name="name"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>