Saturday 2 June 2012

Basics of Javascript


Introduction:

JavaScript is most widely scripting language of the web. JavaScript was designed to add interactivity to HTML pages. It is a lightweight programming language. It is an interpreted language that means scripts execute without preliminary compilation. There is no need to purchase the license for using the JavaScript. The HTML <script> tag is used to insert a JavaScript into an HTML page.

Note: Before learning JavaScript you should have the basic knowledge of HTML and XHTML.

Features of JavaScript:
  • The JavaScript is the third generation language, like C, Pascal, and BASIC. 
  • It is free formatted with C like syntax, not line by line like FORTRAN.
  • It contains a garbage collector and there are no pointers.
  • It has null and undefined special value, like Perl.
  • It has flexible statement and function, and no need of main ().
  • It is hardware independent and highly portable.
JavaScript is mostly widely used for form validation where as the forms fields are checked if they are empty. Most common example is checking for valid email address.

Another common use we see of it is rendering of the page to the client browser based on the browser version the client is using.

We will see the various use of JavaScript in the forthcoming articles.

Example: In this example you will see how can you write the text in JavaScript.

<html>
<head><title>My first JavaScript page</title></head>
<script type="text/javascript">
    document.write("Hello World!")
</script>
    <body>
        <p>
            <b>
                <font color=red>
                    <h1>hello! how are you?</h1>
                </font>
            </b>
        </p>
    </body>
</html>

Output:

 

To insert JavaScript into HTML page we use the <script> tag and we also use the type attribute to define the scripting language.

For Example:

<html>
    <head>
        <title>My first JavaScript page</title>
    </head>
    <body>
        <script type="text/javascript">
            document.write("Hello World!")
        </script>
    </body>
</html>

Document.write is a JavaScript command. It is used for writing the output to a page. By using the document.write command between the <script type="text/javascript"> and </script> tags, the browser will recognize it as a JavaScript command and execute the code line.

If we comment the <script> tag then what will happen:

If we will not  use the <script></script> tag then the browser would have treated the document.write("Hello World!") command as pure text, and just write the entire line on the page.

For Example:

<html><head><title>My first JavaScript page</title></head>
<%--<script type="text/javascript">--%>
document.write("Hello World!")
<%--</script>--%>
<body>
<p><b><font color=red><h1>hello! how are you?</h1></font></b></p>
</body>
</html>

Output: The output of the above code is as follows:

 

In this figure document.write("Hello World!") is displayed as a simple line.

No comments:

Post a Comment