You are on page 1of 8

Introducti on to Java-script: Thursday, 10 February 2011

HTML is a simple text markup language, it can’t respond to the user, make decisions ,or automate repetitive
tasks. Interactive tasks like these require a more complex language: a programming language, or a scripting
language.

Some programming languages must be compiled, or translated into machine code, before they can
be executed. JavaScript, on the other hand, is an interpreted language: The browser executes each
line of script as it comes to it.
Ex .JAVASCRIPT uses a interpreter called Browser.

*Note: JavaScript was originally called LiveScript and was first introduced in Netscape Navigator 2.0
in 1995. It was soon renamed JavaScript to indicate a marketing relationship with Java.

Here are a few of the things you can do with JavaScript:


1.Scrolling and Changing messages.
2.Validate and maintain forms calculation.
3.Display message alert to user which is part of web page.
4.Animated images and change images with mouse movement.
5.Detect browser ,Plugins and notify if any requirement.

To add JavaScript to a page, you’ll use a similar tag: <script>.


Simple Example:
<Body>
<script LANGUAGE=”JavaScript” type=”text/javascript”>
document.write(document.lastModified);
</script>
</Body>

There are actually four different places where you might use scripts:
• In the body of the page. In this case, the script’s output is displayed as part of the HTML document when the
browser loads the page.

• In the header of the page, between the <head> tags. Scripts in the header can’t create output within the HTML
document, but can be referred to by other scripts. The header is often used for functions—groups of JavaScript
statements that can be used as a single unit.

• Within an HTML tag, such as <body> or <form>. This is called an event handler and allows the script to work
with HTML elements. When using JavaScript in Event handlers, you don’t need to use the <script> tag.

• In a separate file entirely. JavaScript supports the use of files with the .js extension containing scripts; these
can be included by specifying a file in the <script> tag.
Example.
<script language=”JavaScript” type=”text/javascript” src=”filename.js”>
</script>
*Note: External JavaScript files have a distinct advantage: you can link to the same.js file from two or more HTML documents. Since the
browser stores this file in its cache, this can reduce the time it takes your Web pages to display.
Browsers and JavaScript

 Display of the Html and javascript varies according to browser to browser.

 ECMA-European computer manufacturing association has finalised ECMA-script


specification-262.

 Another language you may hear of is JScript. This is how Microsoft refers to their
implementation of JavaScript, which is generally compatible with the Netscape version.

 Microsoft’s Common Language Runtime (CLR), part of the .NET framework, supports
JavaScript.

Alternatives to JavaScript
Java:
 Java is a programming language developed by Sun Microsystems that can be used to create
applets, or programs that execute within a Web page.

 Java is a compiled language, but the compiler produces code for a virtual machine rather
than a real computer. The virtual machine is a set of rules for bytecodes and their meanings ,with
capabilities that fit well into the scope of a Web browser.

 The virtual machine code is then interpreted by a Web browser. This allows the same
Java applet to execute the same way on PCs, Macintoshes, and UNIX machines, and on
different browsers.
1
ActiveX:
 ActiveX is a specification developed by Microsoft that allows ordinary Windows programs
to be run within a Web page.

 ActiveX programs can be written in languages such as Visual C++ and Visual Basic, and they are
compiled before being placed on the Web server.

 ActiveX applications, called controls, are downloaded and executed by the Web browser,
like Java applets. Unlike Java applets, controls can be installed permanently when they are
downloaded, eliminating the need to download them again.

CGI and Server-side scripting:


CGI (Common Gateway Interface) is not really a language, but a specification that allows programs to run on Web servers.
CGI programs can be written in any number of languages, including Perl, C, and Visual Basic.
Creating Simple Java script Saturday, 12 February 2011
 Java script is executed when page is loaded in web browser.
 You won’t need any special software to create JavaScript scripts. In fact, you probably
already have everything you need.
 Java script need text editors which can handle ascii text files.
 you can also use JavaScript to calculate “universal” (UTC) time or local time zone.
Java script automatically convert to UTC time.
(*Note: UTC stands for the French equivalent of Universal Coordinated Time, and is the new name
for the old GMT (Greenwich Mean Time) standard. This is the time at the Prime Meridian, near
London, England.)

<script LANGUAGE=”JavaScript” type=”text/javascript”> …………… </script>

JavaScript commands and variable names are case-sensitive.

<script language="javascript" type="text/javascript">


now = new Date();
localtime = now.toString();
utctime = now.toGMTString();
document.write("<b>LOCAL TIME:<b>"+localtime+"<br>");
document.write("<b>UTC TIME:<b>"+utctime+"<br>");
</script>

Browser automatic convert java-script to executable script.

Adding some new code: To Print hour ,minute and seconds…..


now = new Date();
localtime = now.toString();
utctime = now.toGMTString();
document.write("<b>LOCAL TIME:<b>"+localtime+"<br>");
document.write("<b>UTC TIME:<b>"+utctime+"<br>");
hours = now.getHours();
mins = now.getMinutes();
secs = now.getSeconds();
document.write("<font size='+5'>");
document.write(hours + ":" + mins + ":" + secs);
document.write("</font>");

Q.Why are the <b> and <br> tags allowed in the statements to print the time? I thought HTML tags
weren’t allowed within the <script> tags?
A> Since this particular tag is inside quotation marks, it’s considered a valid part of the script. The
script’s output, including any HTML tags, is interpreted and displayed by the browser. You can use
other HTML tags within quotation marks to add formatting, such as the <font> tags we added for the
large clock display.

How Java script works- Sunday, 13 February 2011


Functions:
Java-script include wide variety of functions. Such as.
document.write(“Testing.”);
*A statement that uses a function, as in the example above, is referred to as a function call.
You can also create your own functions. This is useful for two main reasons: First, you
can separate logical portions of your script to make it easier to understand. Second, and
more importantly, you can use the function several times or with different data to avoid
repeating script statements.

Objects:
 Java script support objects, can store two or more pieces of data at once.
 The items of data stored in an object are called the properties of the object. Properties
contains all the data member of the objects.
 Object can include methods.
 Build in objects: Array ,Strings.
 Browser object that contain various component of browser.
 Custom object(create own Person object).

Events:
Event handlers are scripts that handle events.
Event handlers are associated with particular browser objects, and you specify the event handler in
the tag that defines the object. For example, images and text links have an event, onMouseOver,that
happens when the mouse pointer moves over the object. Here is a typical HTML image tag with an
event handler:
<img SRC=”button.gif” onMouseOver=”highlight();”>

Conditional Statement:
JavaScript supports conditional statements, which allow you to answer questions like this. A typical
conditional uses the if statement, as in this example:
if (count==1) alert(“The countdown has reached 1.”);

Loops:
Another useful feature of JavaScript—and most other programming languages—is the
ability to create loops,or groups of statements that repeat a certain number of times. For
example, these statements display the same alert 10 times, greatly annoying the user:
for (i=1;i<=10;i++) {
Alert(“Yes, it’s yet another alert!”);
}

Which Script Runs First?


Priority:1.<Head Script>
2.<Body Script>
3.Event handler script -when that event occurs.

Using Comments:
1.//line code comments.
2./*……Code block Comments.

Functions and variable


 Functions are groups of JavaScript statements that can be treated as a single unit.
 To use a function, you must first define it.
function Greet() { //function is keyword
alert(“Greetings.”);
}

 Your function more flexible, you can add parameters, also known as arguments.
 The best place for a function definition is within the <head> section of the document.

*Note: Without
calling the
function it will do nothing.

So we have to call it on the <body> tag.


Function can also return a value to function caller.

Note: Does not have to mention the return type in function statement at creation time.

Variable in Java script :


Variable are named container that can store -> integer , float, string etc.
JavaScript includes the “var” keyword, which can be used to declare a variable.
There are two types of variables:
• Global variables have the entire script (and other scripts in the same HTML document) as their
scope. They can be used anywhere, even within functions.
• Local variables have a single function as their scope. They can be used only within the function
they are created in.
*Note: Var keyword is optional.
Assigning value to variable:
Use the equal sign(=) to assign a value to a variable.
Java script allows this:
lines = 40;
lines = lines + 1;
lines += 1;
lines++;

Data types in java script:


Numbers Support both integer and floating point numbers
Boolean True / False
Strings one or more characters of text.
Null Using null keyword.

Converting Between data types:


JavaScript interpreter automatically converts any non-strings in the expression.

In some situations, you may end up with a string containing a number, and need to con-
vert it to a regular numeric variable. JavaScript includes two functions for this purpose:
• parseInt() converts a string to an integer number.
• parseFloat() converts a string to a floating-point number.

Work-Shop:

You might also like