Javascript
Javascript
JavaScript contains a standard library of objects, such as Array, Date, and Math,
and a core set of language elements such as operators, control structures, and
statements.
Variable data types are not declared Variable data types must be declared
(dynamic typing). (static typing).
Java is a class-based programming language designed for fast execution and type
safety. Type safety means, for instance, that you can't cast a Java integer into an
object reference or access private memory by corrupting Java bytecodes. Java's
class-based model means that programs consist exclusively of classes and their
methods. Java's class inheritance and strong typing generally require tightly
coupled object hierarchies. These requirements make Java programming more
complex than JavaScript programming.
In object - oriented programming languages, real - life objects are represented (or
modeled) using a set of objects , which form an object model . For example, a car
object might represent a car, a basket object might represent a shopping basket,
and a document object could represent a document such as a web page.
Each object can have a set of properties that describes aspects of the object. A car
object might have properties that describe its color or engine size. A basket object
might have properties that describe the number of items it contains or the value
of those items. A document object has properties that describe the background
color of the web page or the title of the page.
Then there are methods, each method describes an action that can be done to (or
with) the object. For example, a method of an object representing a car might be
to accelerate, or to change gear. A method of a shopping basket might be to add
an item, or to recalculate the value of items in the basket. A method on a
document could be to write a new line of text into the web page.
All of the main browsers implement an object model called the Document Object
Model that was devised to represent web pages. In Document Object Model, the
page as a whole is represented using a document object; then the links in that
page are represented using a links object, the forms are represented in a forms
object, images are represented using an image object, and so on.
Get and set properties of a web page such as the background color.
Call methods that perform actions such as writing a new line into a page.
React to events such as a user pressing a Submit button on a form.
There are several other scripting languages that do a very similar job to JavaScript
(such as VBScript orPerl), but JavaScript is the main programming language used
in web browsers
Example
< html >
< body >
< p >
< script type=”text/javascript” >
document.write(“My first JavaScript”)
< /script >
< /p >
< /body >
< /html >
You can also write JavaScript in external documents that have the file extension .js
(just in the same way that you can write external style sheets). This is a
particularly good option because:
If your script is used by more than one page you do not need to repeat the
script in each page that uses it.
If you want to update your script you need only change it in one place.
It makes the XHTML page cleaner and easier to read.
When you place your JavaScript in an external file, you need to use the src
attribute on the < script > element; the value of the src attribute should be an
absolute or relative URL pointing to the file containing the JavaScript.
For example: