Javascript

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Chapter 4

JavaScript

Introduction
JavaScript is a popular web-page scripting language, and is supported by almost everybrowser.
It adds interactivity to web technology pages.
JavaScript is usually embedded directly into HTML pages. Features of JavaScript
It is lightweight, whose implementations allow client-side script to interact with the userand make
dynamic pages.
It is interpreted programming language with object-oriented capabilities.
Open and cross-platform (machine independent).
JavaScript is a case-sensitive language. So the identifiers Time and TIME will convey different
meanings in JavaScript.
So, JavaScript is a client-side scripting language that runs entirely inside the web browser.
Benefits of JavaScript
Following are some of the benefits that JavaScript language possesses to make the website dynamic.
It is widely supported in browser.
It gives easy access to document object and can manipulate most of them.
It can give interesting animations with many multimedia data types.
It is secure language because it can detect the visitor's browser.
It can react to events, read and write HTML elements
It can be used to validate data
JavaScript Rules
JavaScript program contains variables, objects and functions.
Each line is terminated by a semicolon. Blocks of code must be surrounded by curlybrackets.
Functions have parameters which are passed inside parenthesis.
Variables are declared using the keyword var.
Script does not require main function and exit condition. Language Format
JavaScript can be implemented using JavaScript statements that are placed within the
<script>... </script> HTML tags in a web page.
You can place the <script> tags, containing your JavaScript, anywhere within you webpage, but it is
normally recommended that you should keep it within the <head> tags.
The <script> tag alerts the browser program to start interpreting all the text between thesetags as a script.
So, syntax will look as follows:
<script language="javascript" type="text/javascript"> JavaScriptcode
</script> The
script tag takes two important attributes:
Language: This attribute specifies what scripting language you are using. Typically, its value will be
JavaScript. Although recent versions of HTML have phased out the use of this attribute.
Type: This attribute is what is now recommended to indicate the scripting language in useand its value should
be set to "text/javascript".
Take a look at the following code.
<html>
<body>
<script language="javascript" type="text/javascript"> document.write("Hello World!")
</script>
</body>
</html>
This code will produce the following result: Hello World!
Description:
The <script type="text/javascript"> tag tells the browser that whatever comes between thattag and the
coming </script> tag is script.
The type="text/javascript" tells it that it is JavaScript.
The document.write() is the JavaScript standard for writing text to the browser window.
The 'document' clause refers to the HTML webpage (termed a document) as a whole; whatfollows ('.write()')
is a command for the document object to carry out. JavaScript variables
JavaScript is used for the manipulation of data. The pieces of data can be stored either in variables or arrays.
JavaScript Variables are declared using the “var” keyword. Syntax: var num;
JavaScript variables can be declared with initial value. Syntax: var num=1;
Multiple JavaScript variables can even be declared at the same time.
Syntax: var num=1; var name= “Bikila”;
Every JavaScript variables ends with semicolon (;).
Variables in JavaScript are weakly typed, meaning that the types of individual variables are not determined
by the programmer. Unlike many languages, JavaScript only provides a generic “var” rather than separate
types for integers, floating point numbers, characters, and strings.
Example: var x = 0;
x is a number
Data types and primitives
There are six data types in JavaScript:
Numbers: – Integer or floating point numbers
Booleans: – Either true/false or a number (0 being false) can be used for boolean values
Strings: – Sequence of characters enclosed in a set of single or double quotes
Objects: – Entities that typically represents elements of a HTML page
Null: – No value assigned which is different from 0
Undefined: – Is a special value assigned to an identifier after it has been declared but beforea value has been
assigned to it
JavaScript Popup Boxes
JavaScript has three kinds of popup boxes:
Alert Box: - is used if you want to make sure information comes through to the user.
Syntax: alert("sometext");
Confirm Box: - is used if you want the user to verify or accept something. When aconfirm 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 returnsfalse.
Syntax: confirm("sometext");
if (confirm("Press a button!")) { txt
= "You pressed OK!";
} else { txt = "You pressedCancel!";
}
Prompt Box: - is 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 inputvalue. If the user clicks "Cancel" the box returns null.
Syntax: prompt("sometext","defaultvalue"); var person = prompt("Please enter your name", "Nahil"); if
(person ==null || person == "") { txt = "User cancelled the prompt.";
} else { txt = "Hello " + person + "! How are youtoday?";
}
Operators in JavaScript
Arithmetic Operators

Logical Operators
Assignment Operators: used to assign value to variable

Comparison Operators

String Operator: - is used to concatenate two strings together. Example: var x


= "Hello " + "world!"
Functions in JavaScript
A function consists of the “function” keyword followed by the name of the function, a set of open and close
parentheses enclosing an optional parameter list and a body enclosed in a set of curly braces.
Syntax: function functionName(parameterList) {
// body
}
Function parameters are separated by commas in the function declaration.
A function uses the return keyword to return a value from a function.
JavaScript code found in a function is not executed until the function is called.
Assigns function to event: - many elements of DOM support events. These events are normallythe result of
some user actions.

Example:
<html>
<body>
<script type="text/javascript">function addition(){ var x=7; var y=10; var sum=x+y; alert(sum);} </script>
<form>
<input type="button" onclick="addition()" value="show">
</form> </body></html>
Objectsin JavaScript
JavaScript is not a pure object oriented programming language, but uses the concept ofobjects. The new
keyword used here is to create an object, it allocates memory and storage.
Objects can have functions and variables. To differentiate between global variables and those which are part
of an object but may have the same name,
JavaScript uses this keyword. When referring to a property of an object, whether a methodor a variable, a
dot is placed between the object name and the property.
Regular Expression:
A script language may take name data from a user and have to search through the string one character at a
time. The usual approach in scripting language is to create a pattern calleda regular expression which describes a
set of characters that may be present in a string. The regular expression can be shown below.
var pattern = new RegExp(“target”); varstring = “can you find the target”; pattern.test(string);
Regular expression is a javascript object. Dynamic patterns are created using the keyword new.regex = new
RegExp(“feroz | amer”);
Example 5: JavaScript code to implement RegExp
<html>
<head> <title> registration form</title>
<script type="text/javascript"> functionv()
{
var a=document.f.un.value; varb=document.f.pw.value; var r1=new RegExp("^[a-zA- Z]{6,}$"); var r2=new
RegExp("^[a-zA-Z0-
9]{6,}$");
if(r1.test(a))
{ if(r2.test(b))
{ window.alert("ur successfullylogin");
} else window.alert("enter password atleast 6characters ");
} else { window.alert("enter uname atleast 6 characters and should contain onlyalphabets"); }
}
</script> </head>
<body bgcolor="#5555" text="white"onSubmit="return v()">
<form name="f">
<center>
<table border="0"><br><br><br><br><br><br>
<tr>
<td align="center"> username:</td>
<td align="center"><input type="text" value="" name="un"/></td>
</tr>
<tr>
<td align="center">password:</td>
<td align="center"><input type="password" value="" name="pw"/></td></tr>
<tr> <td align="center" colspan="2"><input type="submit" value="Submit">
<input type="reset" value="reset"/></td></tr>
</table></center></form> </body> </html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy