Wednesday, 2 October 2013

Why javascript & how to use javascript

Introduction

 

JavaScript is the most popular scripting language (a scripting language is a lightweight programming language) on the internet, and works in all major browsers, such as Internet Explorer, Firefox, Chrome, Opera, and Safari.

JavaScript was released by Netscape and Sun Microsystems in 1995.However, JavaScript is not the same thing as Java.

  • It designed to add interactivity to HTML pages
  • ItIs a scripting language (a scripting language is a lightweight programming language)
  • JavaScript code is usually embedded directly into HTML pages
  • JavaScript is an interpreted language (means that scripts execute without preliminary compilation)

 

Uses of JavaScript

 

  • Use it to add multimedia elements. With JavaScript you can show, hide, change, resize images, and create image rollovers. You can create scrolling text across the status bar.
  • Create pages dynamically Based on the user's choices, the date, or other external data, JavaScript can produce pages that are customized to the user.
  • Interact with the user It can do some processing of forms and can validate user input when the user submits the form.

 

Getting Started

 

JavaScript code is typically embedded in the HTML, to be interpreted and run by the client's browser. Here are some tips to remember when writing JavaScript commands.

  • JavaScript code is case sensitive
  • White space between words and tabs are ignored
  • Line breaks are ignored except within a statement
  • JavaScript statements end with a semi- colon ;

The HTML <script> tag is used to insert a JavaScript into an HTML page.Inside the <script> tag we use
the type attribute to define the scripting language. So, the <script type="text/javascript">and </script> tells where the JavaScript starts and ends:


Example given bellow:

<html>
<body>
<script type="text/javascript">
alert("Hello World!");
</script>
</body>
</html>


Where Do You Place Scripts?
 
  • Scripts can be in the either <head> section or <body> section
  • Convention is to place it in the <head> section
<html>
<head>
<script type="text/javascript">
....
</script>
</head>
<body>
<!-- your content -->

</body>
</html>

 

Implementing JavaScript

There are three ways to add JavaScript commands to your Web Pages.
  • Embedding code
  • Inline code
  • External file

 

Embedding code 

you can Embedding javascript commands in html tags .no need to write <script> tag

Example of Embedding javascript code.

<html>
<body onload="alert('hello world ! ');">

<!-- your content -->

</body>
</html>

Note: here onload() is a javascript event which will call at the time of browser load.
Chick here for more about java script events.

Inline code

Write javascript code in the html file by the help of  <script>  tag .
Example of Inline javascript code.


<html>
<body">

<script type="text/javascript">
alert("hello world!");
</script>

<!-- your content -->

</body>
</html>

External File

If you want to run the same JavaScript on several pages, without having to write the same script on every page, you can write a JavaScript in an external file.

Save the external JavaScript file with a .js file extension.
Note: The external script cannot contain the tags!

 Example of external js file bellow. Create a abc.js file and write following codes.

alert("hello world !");

Now create a xyz.html file and call  abc.js file by writting following code.

<html>
<body>
<script type="text/javascript" src="abc.js"></script>
</body>
</html>

I hope this tutorial will helps you for better understanding about javascript and how to use javascript. click here for more about javascript programming basics and syntax.

No comments:

Post a Comment