| Overview |
| |
In this chapter you will learn :
- Basic concepts of JavaScript.
- Location of the <SCRIPT> tag in an HTML document.
- Writing a simple JavaScript program.
|
| |
|
Basic JavaScript Concepts
JavaScript is embedded into an HTML file. A browser reads the HTML files and interprets HTML tags, but the browser needs to be informed that specific sections of HTML code is JavaScript. The browser is given this information using the tags <SCRIPT>......</SCRIPT>. The browser will then use its built-in JavaScript engine to interpret this code.
| Syntax |
<html>
<head>
<Script Language = "JavaScript">
JavaScript code is written here.
</Script>
</head>
</html> |
| Note |
| If the Language attribute is left undefined then the default scripting language of Netscape is JavaScript, whereas Internet Explorer has VB Script as its default scripting language. |
There can be three different locations where the script tag can be placed in the HTML document :
In the Head Section
As the head loads before the body, it is guaranteed that it is available when needed. Hence, it reduces the scope of errors.
| Example |
<html>
<head>
<Script Language = "JavaScript">
In this example, the script tag has been placed inside the head section.
</Script>
</head>
</html> |
In the Body Section
Place the script tag in the body section if you want to generate certain content on the page when it loads.
| Example |
<html>
<body>
<Script language = "JavaScript">
document.write ("Here script tag has been placed inside the body section ");
</Script>
</body>
</html> |
| Note |
| You can place the script tag at both places, i.e., inside the head section as well as the body section. |
In
an External File
When you want to use the same script on different pages, you can use the external file. The advantage of using external file is that you don't need to rewrite it. External JavaScript file is saved with a '.js' extension.
| Example |
<html>
<head>
<Script = "myexample.js"> </Script>
</head>
<body>
</body>
</html> |
Write your First JavaScript Program
| Program |
<html>
<head>
<title>My first program </title>
</head>
<body>
<Script Language = "JavaScript">
document.write(" My first program ");
</Script>
</body>
</html> |
Document is a part of Document Object Model (discussed later in this tutorial). Document refers to all the text and HTML elements between the two BODY tags. Write( ) is a method of Document. So the document.write is a standard JavaScript command for writing output to a page. The language attribute indicates the scripting language used for writing the piece of scripting code.
| |
| |
| Summary |
| |
In this chapter you have learnt:
- Writing JavaScript into HTML.
- All JavaScript statements must be included in the <SCRIPT>...</SCRIPT> tag.
- Different locations where SCRIPT tag can be placed.
- Advantage of creating external JavaScript file.
|
| |
| |
| Review Questions |
| |
Fill in the Blanks
- JavaScript is embedded between -------- tags of HTML.
- The extension of external JavaScript file is ---------.
Solutions
- SCRIPT
- .js
True or False
- <SCRIPT> tag should always be placed in the head section to reduce errors.
- External JavaScript file can contain HTML tags.
- It would be an error if you place <SCRIPT> tag at both head section as well as inside body section also.
Solutions
- False
- False
- False
|
| |
| What's Next |
The next chapter will acquaint you with the concepts of JavaScript operators and variables. The chapter will further elucidate how to create variables and different types of operators available in JavaScript.
Hop over to the next chapter to get the close-up of JavaScript operators and variables.
|
| |
|
|