You can include <SCRIPT> tags anywhere in your HTML document. It is a good idea to define functions in a SCRIPT tag in the header portion of your document, since then the functions will be available to the document as it is displayed. You can also use SCRIPT tags in the body of your document to define JavaScript code to be executed in that part of the document. You can use a NOSCRIPT tag to provide content that is displayed by browsers for which JavaScript is not available, while being ignored by browsers that can understand JavaScript. For example, you could use the NOSCRIPT tag to provide a warning that the page requires JavaScript.
LANGUAGE="languageName"
Syntax
<SCRIPT
LANGUAGE="languageName
"
SRC="location
"
>
...
</SCRIPT>
specifies the program language. If the LANGUAGE attribute is not specified, the default value is JavaScript.
SRC="location"
specifies the URL of the script, if you want to load a script from a separate file. The suffix on a location specifies the scripting language. The suffix .js indicates a JavaScript file. The web server maps the suffix to the MIME time. For JavaScript, the MIME type is "application/x-javascript". Navigator 3.0
<HTML>
<HEAD><TITLE>Script Example</TITLE>
</HEAD>
<SCRIPT language="JavaScript">
function changeBGColor (newcolor) {
document.bgColor=newcolor;
return false;
}
</SCRIPT>
<BODY >
<P>Select a background color:</P>
<FORM>
<INPUT TYPE="button" VALUE=blue onClick="changeBGColor('blue');">
<INPUT TYPE="button" VALUE=green onClick="changeBGColor('green');">
</FORM>
<NOSCRIPT><I>Your browser is not JavaScript-enabled.The file script.htm shows this example in action in a separate window.
These buttons will not work.</I>
</NOSCRIPT>
Example 2. Using SCRIPT tags in the Document Body
This example uses a SCRIPT tag to use JavaScript code to dynamically decide what to write depending on which browser the user is using. The JavaScript code determines the user's browser type and version and writes content depending on the result. The document contains more HTML content following the SCRIPT tag.
This example uses properties and methods on the navigator and document objects. It uses the write() method of the document object to dynamically write content to the web page. For details of the navigator and document objects, see Chapter 3. Using Navigator Objects in the JavaScript Guide .
<H1>Welcome to The Script Example Page</H1>
The file script2.htm shows this example in action in a separate window.
<!-- run some JavaScript code now -->
<SCRIPT language="javascript">
var navVersion = navigator.appVersion;
var navName = navigator.appName;
document.open();
document.write("<P>You are using a " + navName +
" web browser, version " + navVersion + "</P>");
if (navName=="Netscape") {
document.write("<H3>Thank you for using Netscape's Navigator to
browse the web. </H3>");
}
else {
document.write("<H3>Have you tried using <I>Netscape Communicator
</I> to browse the web?</H3>");
}
document.close();
</SCRIPT>
<P>More HTML content goes here...</P>
Example 3: Using the SRC attribute
The following example uses the SRC attribute to specify the location of a file containing JavaScript code:
<SCRIPT LANGUAGE="JavaScript"
SRC="http://www.mycompany.com/scripts/myScript.js">
</SCRIPT>
<NOSCRIPT>...</NOSCRIPT>
<SERVER>...</SERVER>
<P>Your IP address is <SERVER>write(request.ip);</SERVER>
Last Updated: 11/19/97 22:46:33