HTML and CSS Reference
In-Depth Information
1.11 Where to Put JavaScript
Before learning JavaScript, you should be familiar with HTML and how to create an
HTML document. This doesn't mean that you have to be an expert, but you should be
familiar with the structure of HTML documents and how the tags are used to display
various kinds of content on your browser. Once you have a static HTML document, then
adding basic JavaScript statements is quite easy. (Go to http://www.w3schools.com for an
excellent HTML tutorial.) In this text we have devoted a separate chapter to CSS. CSS
allows you to control the style and layout of your Web page by changing fonts, colors,
backgrounds, margins, and so on in a single file. With HTML, CSS, and JavaScript you
can create a Web site with structure, style, and action.
Client-side JavaScript programs are embedded in an HTML document between
HTML head tags <head> and </head> or between the body tags <body> and </body> .
Many developers prefer to put JavaScript code within the <head> tags, and at times, as
you will see later, it is the best place to store function definitions and objects. If you want
text displayed at a specific spot in the document, you might want to place the JavaScript
code within the <body> tags (as shown in Example 1.5). Or you might have multiple
scripts within a page, and place the JavaScript code within both the <head> and <body>
tags. In either case, a JavaScript program starts with a <script> tag, and ends with a
</script> tag. And if the JavaScript code is going to be long and involved, or may be
shared by multiple pages, it should be placed in an external file (text file ending in .js )
and loaded into the page. In fact, once you start developing Web pages with JavasScript,
it is customary to separate the HTML/CSS content from the programming logic (Java-
Script) by creating separate files for each entity.
When a document is sent to the browser, it reads each line of HTML code from top to
bottom, and processes and displays it. As JavaScript code is encountered, it is read and exe-
cuted by the JavaScript interpreter until it is finished, and then the parsing and rendering
of the HTML continues until the end of the document is reached.
EXAMPLE 1.5
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head><title>First JavaScript Sample</title></head>
4 <body bgcolor="yellow" text="blue">
5 <script type="text/javascript">
document.write("<h2>Welcome to the JavaScript World!</h2>");
6 </script>
7 <big>This is just plain old HTML stuff.</big>
8 </body>
9 </html>
 
 
Search WWH ::




Custom Search