HTML/Training/Script

From Web Education Community Group
< HTML‎ | Training
Revision as of 15:05, 18 November 2011 by Rworth (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Script

A client-side script is a program that may accompany an HTML document or be embedded directly in it. Scripts offer you a means to extend HTML documents in highly active and interactive ways.

Any script language may be used with HTML. This educational materials use the script language "JavaScript".


Adding script to HTML

External script

Script files are imported to HTML documents by the src attribute for the script element.


[Syntax]

 <script type="text/javascript" src="example.js"></script>

Local script

The crient-side script is embedded directly in HTML documents. The script should be contained between <script> ... </script>.


[Syntax]

 <script type="text/javascript">
   document.write('Hello world!');
 </script>


See also The script element.


Setting the default script language

You must specify the script language associated with an HTML document by <meta http-equiv="Content-Script-Type">.


[Syntax]

 <meta http-equiv="Content-Script-Type" content="text/javascript">


Designing documents for user agents that don't support scripting

The noscript element

The <noscript> element represents nothing if scripting is enabled, and represents its children if scripting is disabled.


[Syntax]

 <script type="text/javascript">
   document.write('Hello world!');
 </script>
 <noscript>
   Hello world!
 </noscript>


See also The noscript element.


Commenting scripts

The JavaScript engine allows the string "<!--" to occur at the start of a SCRIPT element, and ignores further characters until the end of the line. JavaScript interprets "//" as starting a comment extending to the end of the current line. This is needed to hide the string "-->" from the JavaScript parser.


[Syntax]

 <script type="text/javascript">
 <!--  to hide script contents from old browsers
   document.write('Hello world!');
 // end hiding contents from old browsers  -->
 </script>
 <noscript>
   Hello world!
 </noscript>


Congratulations!
You finished the HTML educational materials for beginners.
Next step, CSS educational materials for beginners.