Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Note that the extension of the external file must be “.js”, just as the extension of an HTML file must be “.html” or “.htm”. The name of the file does not have to include the full path—a relative (virtual) path is enough.

Suppose abc1.js contains the following code:

var counter = 100

function alertMe(message) {
 alert(message)
}

function writeMe(message) {
 document.write(message)
}

Do not pay any attention to the meaning of the statements. They are all explained in the following chapters. Here’s the basic HTML file:

<HTML>
<HEAD>
<TITLE>Using external files</TITLE>
<SCRIPT LANGUAGE="JavaScript" SRC="abc1.js">
<!--

var digit = 8
alertMe("Hello!")
writeMe("Howdy!")

// -->
</SCRIPT>
</HEAD>
</htmL>

The preceding structure is equivalent to the following HTML document:

<HTML>
<HEAD>
<TITLE>Equivalent Script</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--

var counter = 100

function alertMe(message) {
 alert(message)
}

function writeMe(message) {
 document.write(message)
}

var digit = 8
alertMe("Hello!")
writeMe("Howdy!")

// -->
</SCRIPT>
</HEAD>
</htmL>

It is extremely important to understand the way external scripts are interpreted, because many different rules apply to JavaScript and HTML layout (this topic is discussed in the following chapter). Just keep in mind that an external script is actually interpreted as if it precedes the local script (enclosed by the <SCRIPT> and </SCRIPT> tags). The <!-- and // --> tags are introduced in the next section.

JavaScript-only files should have the filename suffix “.js”, and the server must map the “.js” suffix to the MIME type “application/x-javascript”, which it sends back in the HTTP header. If the server does not map the .js filename extension to application/x-javascript, Navigator will not load properly the JavaScript file specified by the SRC attribute. In short, your server must be configured to reflect the proper MIME type—otherwise the browser does not respond properly with the data coming back in the HTTP response to the SRC-initiated request.

Hiding the Script

You have probably asked yourself what happens when someone loads your page with an old browser that does not support JavaScript. The solution to this problem is based on the commenting tags of both HTML and JavaScript.

Commenting tags in Netscape Navigator are <!-- and -->. The first one opens the comment block, and the second one closes it. A simple comment in a plain HTML document looks like this:

<!-- copyright 1997 -->

There are two types of comments in JavaScript:

  • // to begin a short comment that does not exceed the length of one line
  • /* and */ to enclose a comment of any length

The JavaScript interpreter ignores the opening HTML comment tag in JavaScript code. Take a look at the syntax you should use to hide the code from old browsers that do not support JavaScript:

<SCRIPT LANGUAGE="JavaScript">
<!-- hide code from old browsers

JavaScript statements...

// end code hiding -->
</SCRIPT>

In order to understand how this hiding works, picture the output of the preceding script in a browser that does not support JavaScript. Both the <SCRIPT> and the </SCRIPT> tags are ignored, like any other unrecognized tag. Everything between the HTML commenting tags is also ignored. This structure works fine with Navigator and IE, which understand the tags and interpret the script correctly.

Problems with Code Hiding

It is important to understand that Netscape Navigator does not use standard HTML. One difference is the commenting tag. While Netscape Navigator expects the sequence <!-- comments -->, standard HTML commenting syntax is <! comments >. Therefore, any greater-than logical operator in the JavaScript source indicates the end of a comment. The script statements beyond this character are interpreted as noncomment plain HTML code, causing the JavaScript statements to be printed to the page as plain text. The only workaround is to avoid using the > character in the script. For instance, you can replace b > a with a < b. In some situations, such as printing an HTML tag via JavaScript, it is more difficult to find a replacement for the greater-than character. Take a look at the following script:

<SCRIPT LANGUAGE="JavaScript">
<!--

document.write("<TABLE>")

// -->
</SCRIPT>

An old browser begins to interpret the code as a plain HTML comment when it encounters the <! sequence. It terminates the comment when it finds a > character which is present in the script itself. Therefore, an old browser will print the remaining portion of the script. Generally speaking, you must avoid providing a greater-than character literally in a script. The perfect workaround in this case is to assign the greater-than character to a variable without placing it literally in the script, and then evaluating the variable when needed. That would be as follows:

<SCRIPT LANGUAGE="JavaScript">
<!--

var gt = unescape("%3E")
document.write("<TABLE" + gt)

// -->
</SCRIPT>

Scan the script and notice that no greater-than characters are present. See Chapter 16, Handling Strings, for details on the unescape() function.

Alternate Content

A new HTML tag which enables you to provide alternate content for users who are using old browsers or who have disabled JavaScript is the <NOSCRIPT> tag. This tag and its closing counterpart </NOSCRIPT> are similar to the <NOFRAMES> tag which is used to provide alternate content to users who are not using a frame-enabled browser. The following example demonstrates how to add such content for users who don’t run JavaScript:

<NOSCRIPT>

<B>This page uses JavaScript, so you need to get
Netscape Navigator 2.0 or later!</B><BR>

<A HREF="http://home.netscape.com/comprod/mirror/index.html">
<IMG SRC="NSNow.gif"></A><BR>

If you are using Navigator 2.0 or later, and you see this message,
you need to enable JavaScript by choosing Options | Network
Preferences.

</NOSCRIPT>

You might be asking yourself how an old browser that does not even recognize scripts can interpret the <NOSCRIPT>…</NOSCRIPT> portion correctly. A JavaScript-enabled browser simply ignores that section when JavaScript is currently enabled. However, an old browser does not know this tag and ignores it. Therefore, it does not ignore the interior of a <NOSCRIPT>…</NOSCRIPT> portion, causing the HTML you put in between to be interpreted only by an old browser or one that has its JavaScript support disabled.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us