Warning:
This wiki has been archived and is now read-only.

Educational materials/Script

From HTML Wiki
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="http://www.example.com/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>