HTML and CSS Reference
In-Depth Information
In earlier versions of HTML, the language attribute was used in place of the type
attribute to indicate the script language. Thus if you are working with legacy pages you
might see the following script element used to embed a JavaScript program:
ƒƒƒ JavaScriptƒcommands
The language attribute has been deprecated and is not supported in either HTML5 or
XHTML, so you should use the type attribute in its place if you want to conform to the
current standards.
Note that the script element can be used with programming languages other than
JavaScript. Other client-side scripting languages are identified by using a different value
for the type attribute. For example, if you use VBScript from Microsoft, the MIME type is
text/vbscript. You won't use VBScript in this tutorial.
Embedding a Script
• To place a script element in a Web page, insert the two-sided tag
<scriptƒtype=” mime-type ”>
ƒƒƒ scriptƒcommands
where mime-type defines the language in which the script is written and
scriptƒcommands represents commands written in the scripting language.
• For JavaScript programs, set mime-type to text/javascript .
Placing the script element
When a browser encounters a script element within a file, it treats any lines within
the element as commands to be run, processing the commands in the order in which
they appear within the file. There is no limit to the number of script elements that you
can use within a Web page. Scripts can be placed in either the head section or the body
section of a document. When placed in the body section, a browser interprets and runs
them as it loads the different elements of the Web page. Although a single page can
contain many script elements, the browser still works with them as a single unit. As a
result, JavaScript content that is placed in one script element can be referenced within
other script elements in the HTML file.
Writing a JavaScript Statement
Now that you've reviewed some of the basic concepts surrounding JavaScript, you'll
examine how to enter JavaScript code. Every JavaScript program consists of a series of
statements. Each statement —also known as a command —is a single line that indicates
an action for the browser to take. A statement should end in a semicolon, employing the
following syntax
where JavaScriptƒstatement is the code that the browser runs. The semicolon is
the official way of notifying the browser that it has reached the end of the statement.
Most browsers are very forgiving and still interpret most statements correctly even if you
neglect to include the ending semicolon. However, it is good programming practice to
include the semicolons, and some browsers require them.
Search WWH ::

Custom Search