Вы находитесь на странице: 1из 8

INTRODUCTION TO JAVASCRIPT:

JSP uses Java technology to build pages on the server which are then served to a client;
Javascript is a client-side scripting technology.
JavaScript is a scripting language designed primarily for adding interactivity to Web
pages and creating Web applications.
A scripting language is a lightweight programming language that is interpreted by the
browser engine when the web page is loaded.
JavaScript runs in the client, that is, the browser. If you use an older browser without
support for JavaScript or if you simply choose to disable JavaScript in your browser, then
a JavaScript script can't work.
JavaScript is programming code that can be inserted into HTML pages and can be
executed by all modern web browsers.
The language was first implemented by Netscape Communications Corp. in Netscape
Navigator 2 beta (1995).
JavaScript is different from the Java language (developed in the 1990s at Sun
Microsystems).
Client-side JavaScript programs, or scripts, can be embedded directly in HTML source of
Web pages.
JavaScript is the world's most popular programming language. It is the language for
HTML and the web, for servers, PCs, laptops, tablets, smart phones, and more.
JavaScript does not have classes in the C++ or Java . In JavaScript, objects can inherit
properties directly from each other.
JavaScript is an object-oriented language with prototypal inheritance. The language
supports several built-in objects, and programmers can create or delete their own
objects
Prototypal inheritance makes JavaScript very different from other popular programming
languages such as C++, C#, or Java featuring classes and classical inheritance.
JavaScripts official name is ECMAScript, which is developed and maintained by
theECMA (European Computer Manufacturer's Association) International organization.

Advantages of JavaScript:
An Interpreted Language: JavaScript is an interpreted language, which requires no compilations steps.
This provides an easy development process. The syntax is completely interpreted by the browser just as
it interprets HTML tags.
Embedded within HTML: JavaScript does not require any special or separate editor for programs to
be written, edited or compiled. It can be written in any text editor like Notepad, along with appropriate
HTML tags, and saved as filename.html.
Minimal Syntax - Easy to Learn: Be learning just a few commands and simple rules of syntax,
complete applications can be built using JavaScript.
Quick Development: Because JavaScript does not require time-consuming compilations, scripts can be
developed in a short period of time.
Designed for Simple, Small Programs: It is well suited to implement simple, small programs (for
example, a unit conversion calculator between miles and kilometers, or pounds and kilograms). Such
programs can be easily written and executed at an acceptable speed using JavaScript. In addition, they
can be easily integrated into a web page.
Performance: JavaScript can be written such that the HTML files are fairly compact and quite
small. This minimizes storage requirements on the web server and downloads time for the client.
Procedural Capabilities: Every programming language needs to support facilities such as Condition
checking, looping and branching. JavaScript provides syntax, which can be used to add such procedural
capabilities to web page coding.
Designed for Programming User Events: JavaScript supports Object/Event based programming.
JavaScript recognizes when a form Button is pressed. This event can have suitable JavaScript code
attached, which will execute when the Button Pressed event occurs.
Easy Debugging and Testing: Being an interpreted language, JavaScript scripts are tested line by line,
and the errors are also listed as they are encountered, i.e. an appropriate error message along with the line
number is listed for every error that is encountered.
Platform Impendence / Architecture Neutral: JavaScript is a programming language that is
completely independent of the hardware on which it works. It is a language that is understood by any
JavaScript enabled browser. Thus, JavaScript applications work on any machine that has an appropriate
JavaScript enabled browser installed. This machine can be anywhere on the network.

Writing JavaScript into HTML Document:
The browser is given this information using the HTML tags <SCRIPT> . </SCRIPT>. The <SCRIPT> tagmark
the beginning of scripting code. The paired </SCRIPT> marks the end of scripting code.
Syntax: <SCRIPT LANGUAGE=JavaScript TYPE= text/javascript>
Javascript code written here
</SCRIPT>

Attributes:
LANGUAGE: Indicate the scripting language used for writing the scripting code.
Values are JavaScript & VBScript.
TYPE: Specifies the media type of the scripting language, e.g., text/javascript.
However, most browsers only support the deprecated LANGUAGE
attribute, which specifies the language name. Examples of supported
LANGUAGE values include JavaScript, JavaScript1.1, and VBScript. The
values are not case sensitive.
SRC: The SRC attribute allows authors to reuse code by specifying an external
script.
JavaScript Syntax:
Variables and Values:
One of the main differences between JavaScript and most other languages is that it does not
have explicit data types. There is no way to specify that a particular variable represents an
integer, a string, or a floating-point (real) number.
All JavaScript variables are declared using the keyword var. A variable may be initialized, meaning
that it is given a value when it is declared, or it may be uninitialized.
For example, the statements
var x = 7
var y,z = "19"
var lk = "lucky"
Implicit Data Types in JavaScript
There are five major implicit data types in JavaScript. A JavaScript value may be as follows:
A number, such as -5, 0, or 3.3333
A string, such as "Click Here" or "JavaScript"
One of the logical values true or false
A "non-atomic" JavaScript element, such as a function or object
The special value null
Actually, it would be more correct to say that there are five categories of data type, since it is
possible to distinguish two different types of numbers (integers and floating-point numbers), and
many different types of JavaScript objects, functions, and other structured types. In fact, part II of
this book, "JavaScript Objects," is entirely devoted to explaining the many different JavaScript objects.

Operators
The set of operators that JavaScript uses is, once again, very similar to that of the C, C++, and Java
languages. It provides a number of different ways of combining different values, both literals and
variables, into expressions.
Computational Operators

+ Addition, String Concatenation
- Subtraction, Unary Negation
* Multiplication
/ Division
% Modulus
++ Preincrement, Postincrement
-- Predecrement,Postdecrement
Logical Operators
==, != Equality, Inequality
<,<=,=>,> Arithmetic and String Comparison
! Logical NOT
&&,|| Logical AND, Logical OR
? Conditional Selection (trinary)
, Logical Concatenation
Bitwise Operators
&,|,^,~ Bitwise AND, Bitwise OR Bitwise eXclusive OR
(XOR) Bitwise NOT
<<,>>,>>> Shift Left, Shift Right, Unsigned Shift Right
Assignment Operators
= Assignment
OP= Aggregate Assignment
(+,-,*,/,%,&,|,^,~,<<,>>,>>>)

Comments in JavaScript Code:
C style comments are typically used to document major functions or code blocks. Because a C
comment may extend over multiple lines it is ideal for detailed discussions of important parts of
the code. A C comment begins with /* and ends with */.

Control Structures
At this point, you have had just enough of the JavaScript language to declare variables, perform
assignments, and do various types of arithmetic, string, and logical calculations. You are not yet able to
write any meaningful code because you do not have any higher level constructions. In this section, we
will consider various methods of controlling the way in which statements are executed. The next section
will expose the highest level of JavaScriptits functions and objects.
There are three types of control structure in JavaScript, as follows:
if
while
for


Functions
A function is a block of code that has a name. Whenever that name is used the function is called, which
means that the code within that function is executed. Functions may also be called with values, known
as parameters, which may be used inside the body of the function. Functions serve two purposes. A
function is an organizational tool, in the sense that it permits you to perform the same operation without
simply copying the same code.
The syntax for a function statement in JavaScript is as follows:
function Name ( listofparams )
{
body
}
The function's body is the set of statements that make up the function.
function summation ( endval ) {
var thesum = 0; // this variable will hold the sum
for ( var iter = 1; iter < endval; iter++ ) {
thesum += iter; // add the integer into the sum
} // end of the for loop
return( thesum ); // return the sum
}
Objects
What JavaScript calls an object is called a data structure (or class) in many other languages. A
JavaScript object is made up of a set of component parts, which are called its properties, or members.
Functions are used to provide a uniform method for organizing code. Objects serve the same
purpose for data. Up to this point, the only data items we have seen are simple variables
declared with var. Each of these typeless quantities can only hold a single value of some sort at a
time. Objects provide the ability to hold multiple values, so that a group of related data elements can
be associated with one another.
Built-In Objects

I. String Objects:
String objects are the most built-in of all the built-in JavaScript objects. You do not even use new
when creating a string object. Any variable whose value is a string is actually a string object.
Literal strings such as "HelloWorld" are also string objects.
String Content Methods
The following methods can be used on string objects to access, control, or modify their content:
charAt( idx )
indexOf( chr )
lastIndexOf( chr )
substring( fromidx, toidx )
toLowerCase()
toUpperCase()
The toLowerCase and toUpperCase methods convert the contents of the string entirely to
lower- and uppercase, respectively. So if we define the string variable
var mystr = "Look At This"

String Appearance Methods The string appearance methods are used to control how a string
appears when displayed on a Web page. If you are creating a page with standard HTML tags you
would achieve the same effects by using various tags. For example, to make the string "help"
appear in italics you would write <I>help</I>. The string appearance methods allow you to
obtain the same effects in JavaScript without using the corresponding HTML elements. The
string appearance methods are as follows
big()
blink()
bold()
fixed()
fontcolor(colr)
fontsize(sz)
italics()
small()
strike()
sub()
sup()

II. The Math Object:
The Math object is used for various forms of mathematical calculations. It contains several
properties that are standard constant, such as pi=3.14159, as well as a large set of methods
that represent common trigonometric and algebraic functions.
The math object is our first example of a static object.a static object is one that does not
change. All of the slots in the math object already values.
The Math object has the following methods:
abs( num )
acos( num )
asin( num )
atan( num )
ceil( num )
cos( ang )
exp( num )
floor( num )
log( num )
max( num1, num2 )
max( num1, num2 )
pow( num1, num2 )
random()
round( num )
sin( ang )
sqrt( num )
tan( ang)

III. The Date Object
Dealing with dates is one of the most tedious tasks in any language. This is because many people
like to represent dates and times in decidedly nondecimal systems. Months come in units of 12,
hours in units of 24, and minutes and seconds in units of 60
the date object has no properties, but many methods.you must first understand how to construct
instances of it. There are three basic methods of creating a Date instance, as follows:
new Date()
new Date(datestring)
new Date(yr,mon,day)
the date object has a large set of methods which are following:
getDate()
getDay()
getHours()
getMinutes()
getMonth()
getSeconds()
getTime()
getYear()
setDate()
setHours()
setMinutes()
setMonth()
setSeconds()
setTime()
setYear()

IV. Browser Objects:
The primary browser objects, in rough order of significance, are as follows:
window
document
location
history

a. The Window Object:
Javascript maintains an idea of the current window, so that almost all references to sub-objects of
the current window do not need to refer to it explicitly.window object have the following
interesting methods.
alert(msgstr)
close()
confirm(msgstr)
open(urlstr,wname)
prompt(msgstr)
b. The Document Object:
Every window is associated with a document object. The document object contains properties
for every anchor, link, and form on that page, as well as all of the sub-elements of those
elements. It also contains properties for its title (the content of the <TITLE> field of the page),
its foreground color (the fgColor property), its background color (the bgColor property), its
various link colors, and other attributes of the page itself.
The document object has the following methods:
clear()
close()
open()
write( str )
writeln( str )
The clear method is used to completely erase a document window.
c. The history and location Objects
The history object is used to refer to the history list of previously visited URLs. The history
object has a property known as length, which indicates how many URLs are stored on the
history list at present. It also has the following three methods:
back()
forward()
go( where )
The go method is used to navigate the history list. The where argument can be a number or a
string. If the where argument is a number then it indicates how far we wish to move in the
history list. A positive number means that we wish to move that many documents forward in this
history list, while a negative number is used to move backward. Thus, go(5) has the same effect
as using the Forward button five times, while go(-1) would be the same as clicking the Back
button once. If where is a string representing a URL then that URL is loaded, and
becomes the current document. The location object describes the URL of a document.

V. HTML Objects

The important aspect of this example is not its primitive HTML, but the fact that the HTML elements
in it are reflected in the JavaScript object hierarchy. We have already seen that we can access the title
of this document through the title property of the document object. We can also access the other
HTML elements of this document using the following properties:

anchors
forms
links
These properties of the document object are arrays representing every HTML element that is an anchor,
form, or link on the page. In our particular example there is only one of each, so we would refer to the
anchor at the top of the page as document.anchors[0], the link at the bottom of the page as
document.links[0], and the form in the middle of the page as document.forms[0]. These are the top-level
HTML objects represented by this document. Each of these elements, in turn, has properties and
methods that can be used to describe and manipulate it.
In particular, the form object corresponding to forms[0] has sub-objects for each of the three form
elements (the reset button, the submit button, and the text input field), as well as properties for the
submit method and the submit target. forms[0].elements[0] corresponds to the text input field.
forms[0].elements[0].name is the name of that field, as specified by the NAME field, which is "me" in
this case.

VI. Array Object:
Declaration:
Var a=new Array(3);
Initialization:
A[0]=13;
A[1]=67;
.
So on

Вам также может понравиться