Contents [1ST PART Placing the Code]LINKS TO GO THROUGH IN THE NEXT POST in part 2 |
The script element
All
JavaScript, when placed in an HTML document, needs to be within a script element. A script element is
used to link to an external JavaScript file, or to contain inline scripting
(script snippets in the HTML file). A script element to
link to an external JavaScript file looks like:<script type="text/javascript" src="script.js"></script>
while
a script element that contains inline JavaScript looks like:<script type="text/javascript">
// JavaScript code here
</script>
Inline
scripting has the advantage that both your HTML and your JavaScript are in one
file, which is convenient for quick development and testing. Having your javascript in a
separate file is recommended for javascript functions which can potentially be
used in more than one page, and also to separate content from behaviour.Scripting Language
Thescript element will
work in most browsers, because JavaScript is currently the default scripting
language. It is strongly recommended though to specify what type of script you
are using in case the default scripting language changes.The scripting language can be specified individually in the
script element
itself, and you may also use a meta tag in the head of the document to specify
a default scripting language for the entire page.<meta http-equiv="Content-Script-Type" content="text/javascript" />
While
the text/javascript was formally obsoleted in April 2006 by RFC 4329 [1]
in favour of application/javascript, it is still
preferable to continue using text/javascript due to old
HTML validators and old web browsers such as Internet Explorer 8 which are
unable to understand application/javascript. [2]Inline JavaScript
Using inline JavaScript allows you to easily work with HTML and JavaScript within the same page. This is commonly used for temporarily testing out some ideas, and in situations where the script code is specific to that one page.<script type="text/javascript">
// JavaScript code here
</script>
Inline HTML comment markers
The inline HTML comments are to prevent older browsers that do not understand JavaScript from displaying it in plain text.<script type="text/javascript">
<!--
// JavaScript code here
// -->
</script>
The
"//" is a comment delimiter, which prevents the end
comment tag "-->" from being interpreted as
JavaScript. The "<!--" acts as a one line comment in
JS, in addition to being the start of an HTML comment.The use of comment markers is rarely required nowadays, as the browsers that do not recognise JavaScript are virtually non-existent. These early browsers were Mosaic, Netscape 1, and Internet Explorer 2. From Netscape 2.0 in December 1995 and Internet Explorer 3.0 in August 1996 on, browsers were able to interpret javascript.[3] Any modern browser that doesn't support JavaScript, recognizes the <script> tag and does not display it to the user.
Inline XHTML JavaScript
In XHTML, the method is somewhat different:<script type="text/javascript">
// <![CDATA[
// JavaScript code here
// ]]>
</script>
Note
that both the <![CDATA[ tags are commented out. The // prevents the
browser from mistakenly interpreting the <![CDATA[ as a
Javascript statement (which would be a syntax error).
_____________________________________
No comments:
Post a Comment