Post Fri May 05, 2006 10:07 pm

How to Insert Scripts into HTML

Work with browsers that do not support scripts
This example demonstrates how to handle browsers that do not support scripting.
Insert a Script into HTML Page

A script in HTML is defined with the <script> tag. Note that you will have to use the type attribute to specify the scripting language.
  Code:
<html>
<head>
</head>
<body>

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

</body>
</html>


The script above will produce this output:

Hello World!
Ofcourse you don't usually need to put in head, unless its the header, but always if its a script insert <script type>="blablabla" </script> since its a script. If it wasn't then you would put <h4> (makes the size of the text 4) </h>
but in this case its a script so we don't put that for text.
A browser that does not recognize the <script> tag at all, will display the <script> tag's content as text on the page. To prevent the browser from doing this, you should hide the script in comment tags. An old browser (that does not recognize the <script> tag) will ignore the comment and it will not write the tag's content on the page, while a new browser will understand that the script should be executed, even if it is surrounded by comment tags.
Example
JavaScript:
<script type="text/javascript">
<!--
document.write("Hello World!")
//-->
</script>

VBScript:
<script type="text/vbscript">
<!--
document.write("Hello World!")
'-->
</script>

The <noscript> Tag

In addition to hiding the script inside a comment, you can also add a <noscript> tag.

The <noscript> tag is used to define an alternate text if a script is NOT executed. This tag is used for browsers that recognize the <script> tag, but do not support the script inside, so these browsers will display the text inside the <noscript> tag instead. However, if a browser supports the script inside the <script> tag it will ignore the <noscript> tag.
Example
JavaScript:
<script type="text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
<noscript>Your browser does not support JavaScript!</noscript>

VBScript:
<script type="text/vbscript">
<!--
document.write("Hello World!")
'-->
</script>
<noscript>Your browser does not support VBScript!</noscript>

Script Tags
Tag Description
<script> Defines a script
<noscript> Defines an alternate text if the script is not executed
<object> Defines an embedded object
<param> Defines run-time settings (parameters) for an object
<applet> Deprecated. Use <object> instead