Basic Concepts

In Intoduction part we see about scripting language, JavaScript introduction, and how to write JavaScript with html that one programme. Now let’s learn some facts and more close look in JavaScript.

Ending Statements with a Semicolon is necessary?

    When we use other programming language then in most of language we put Semicolon at end of statement. With traditional programming languages, like C++ and Java, each code statement has to end with a semicolon.
Many programmers continue this habit when writing JavaScript, but in general, semicolons are optional! However, semicolons are required if you want to put more than one statement on a single line.

How to Handle Older Browsers?

Browsers that do not support JavaScript will display the script as page content. To prevent them from doing this, we may use the HTML comment tag as follows:
<script language="JavaScript">
<! -- Document. Write ("Hello JavaScript!")//-->
</script>
The two forward slashes at the end of comment line (//) are a JavaScript comment symbol. This Prevents the JavaScript compiler from compiling the line.

Writing JavaScript Code
JavaScript Where to JavaScript in the body section will be executed WHILE the page loads. JavaScript in the head section will be executed when CALLED.

Where to Put the JavaScript?
JavaScript in a page will be executed 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.

Scripts in the head section: Scripts 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 anyone uses it.
<Html>
<Head>
<script language=”JavaScript">
....</script></head>

Scripts in the body section: Scripts 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.
<html>
<head>
</head>
<body><script language=”javascript">
....</script></body>

Scripts in both the body and the head section: We can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.
<Html>
<Head>
<script language=”JavaScript">
....</script>
</head>
<Body>
<script language=”JavaScript">
....</script>
</body>

Using an External JavaScript in HTML file
Sometimes you might want to run the same JavaScript on several pages, without having to write the same script on every page. To simplify this, you can write a JavaScript in an external file. Save the external JavaScript file with a .js file extension.
To use the external script, point to the .js file in the "src" attribute of the <script> tag:
<Html>
<Head>
<script src="fileName.js">
</script>
</head>
<Body>
</body>
</html>