Monday 14 November 2011

Introduction to JavaScript

JavaScript is a scripting language developed by Netscape that is used to modify web pages. JavaScript is a client side scripting language.

The JavaScript must be written in the HTML document between open <SCRIPT> tag and closing </SCRIPT> tag with an attribute to script known as “Language=JavaScript” or type=”text/JavaScript”.

JavaScript is a freeware programming language so everyone can use JavaScript without purchasing a license.

Ø       What can a JavaScript Do(Use of JavaScript)?

·      JavaScript gives HTML designers a programming tool - HTML author are normally not programmers. But JavaScript is a scripting language with a very simple syntax almost anyone can put small “snippets” of code into their HTML pages.

·      JavaScript can put dynamic text into an HTML page - A JavaScript statement like this: document.write("<h1>" + name + "</h1>") can write a variable text into an HTML page.

·      JavaScript can react to events - A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element.

·      JavaScript can read and write HTML elements - A JavaScript can read and change the content of an HTML element

·     JavaScript can be used to validate data - A JavaScript can be used to validate form data before it is submitted to a server. This saves the server from extra processing

·        JavaScript can be used to detect the visitor's browser - A JavaScript can be used to detect the visitor's browser, and - depending on the browser - load another page specifically designed for that browser

·       JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve information on the visitor's computer

Ø       How to put JavaScript into HTML pages.

·     To insert a JavaScript into an HTML page. we use the <script> tag.so the <script type=”javascript”> and </script> tells where the javascript start and ends:
<script type=”text/javascript”>
         
                   …..

</script>

·         The document.write is a standard javascript command for writing output to a page.

·     By entering the document.write command between the <script type=”javascript”> and </script> tags, the browser will recognize it as a javascript command and execute the code line.


Example

<html>
<body>
          <script language=”javascript”>
                   document.write(“Hello World”);
          </script>
</body>
</html>

The code above will produce this output on an HTML page:

Hello World     

Ø       Where to put javascript

Javascript in a page will be execute immediately while the page loads into the browser. This is not always what we want. sometimes we want to execute a script when a page loads, other times when a user triggers an event.

·         Script in the head section

Script to be executed when they are called, or when an event is triggered go in the head section.
When you place a script in the head section, you will ensure that the script is loaded before uses it.

Example

           <html>
<head>
          <script language=”javascript”>
                   document.write(“Hello World”);
          </script>
</head>
</html>

·         Script in the body section

Script to be executed when the page loads go in the body section.
When you place a script in the body section it generates the content of the page.

Example

           <html>
<body>
          <script language=”javascript”>
                   document.write(“Hello World”);
          </script>
</body>
</html>

·         Script in both the body and the head section

You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.

Example

<html>
          <head>
          <script language=”javascript”>
                   document.write(“Hiiii”);
          </script>
          </head>
<body>
          <script language=”javascript”>
                   document.write(“Hello World”);
          </script>
</body>
</html>

No comments:

Post a Comment