Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Making Peace: Legacy Browsers and External Scripts

Rated 4.13 (Ratings: 6) (Add your rating)

Log in to add a comment
(9 comments so far)

Want more?

 
Picture of jaylard

James Aylard

Member info | Full bio

User since: November 18, 1999

Last login: November 18, 1999

Articles written: 7

There are several benefits to using external JavaScript files, and among them are more-compact HTML pages and the caching and sharing of script files among multiple web pages. Most legacy browsers, meaning those that support only JavaScript version 1 (such as Netscape 2 and Internet Explorer 3), however, do not support external script files. When a web page tries to call a function or access a variable that is stored in an external script file, first-generation JavaScript-enabled browsers will throw a fit of errors.

Rather than dismiss this pitfall as part of the price of using a legacy browser, there are steps that developers can take to prevent errors in older browsers while preserving the benefits of external script files for everyone else. Here are two of them:

Method 1

In this first example, the code for legacy browsers is contained between the opening and closing <script> tags that also reference the external script file through the src attribute:

Example:

<script type="text/javascript" src="scripts/js_ExternalJavascriptSupport.js">
  <--
    function fnMouseOver1() {}
    function fnMouseOut1() {}
    function fnMouseClick1() {
      alert("Your browser does not support external script files.\nSorry!") ;
    }
  // -->
</script>


Partial source of js_ExternalJavascriptSupport.js:

function fnMouseOver1() {
  if (document.images) {
    document.images["Smiley1"].src = "images/SmileyOver.gif" ;
  }
}

function fnMouseOut1() {
  if (document.images) {
    document.images["Smiley1"].src = "images/Smiley.gif" ;
  }
}

function fnMouseClick1() {
  alert("Your browser does support external script files.\nAwesome!") ;
}


Try it:

Smiley Face
  • Mouse over the image above. If you are using a legacy browser you should see no change in the image -- but you will also not receive a scripting error. If you are using a newer browser that understands external script files, the mouseover image will change (assuming that your browser also supports the images collection).
  • Click on the image above. You will receive an alert that confirms whether or not your browser understands external script files. Browsers that do should execute the function in the external file, while browsers that don't should execute the function contained within the embedded script element.


Benefits:
  • With minimal code, this method prevents errors in legacy browsers that do not understand external script files.
  • It takes advantage of the designed intent of the &lt;script&gt; element: browsers that understand the src attribute should ignore any content contained between the opening and closing &lt;script&gt; tags, while browsers that don't understand it will execute that code while ignoring the external file.
  • It works with functions and variables.


Drawbacks:

  • A very small number of known browsers -- all of which are extremely minor makes and models -- are capable of handling external script files while also executing code contained within &lt;script&gt; elements that include a src attribute. This number is far smaller than the number of legacy browsers that will handle this method correctly, but it is still a consideration, however small.

Method 2

The second method was developed by Evolt's Jeff Howden, and offers a benefit or two that the previous method does not. This method involves the use of two &lt;script&gt; elements: one that lacks a src attribute to accommodate legacy browsers, and one that includes a src attribute to handle newer browsers. The secret of this method is that if there are two versions of a function, the second version takes precedence (much like style declaration precedence for those familiar with CSS). So, by declaring the legacy version of the function first, and then the more-robust version of the function in an external script file, newer browsers that understand external script files will execute the second version, while legacy browsers that don't understand them will execute the first.

Example:

<script type="text/javascript">
  <--
    function fnMouseOver2() {}
    function fnMouseOut2() {}
    function fnMouseClick2() {
      alert("Your browser does not support external script files.\nSorry!") ;
    }
  // -->
</script>
<script type="text/javascript" src="scripts/js_ExternalJavascriptSupport.js"></script>


Partial source of js_ExternalJavascriptSupport.js:

function fnMouseOver2() {
  if (document.images) {
    document.images["Smiley2"].src = "images/SmileyOver.gif" ;
  }
}

function fnMouseOut2() {
  if (document.images) {
    document.images["Smiley2"].src = "images/Smiley.gif" ;
  }
}

function fnMouseClick2() {
  alert("Your browser does support external script files.\nAwesome!") ;
}


Try it:

Smiley Face
  • Mouse over the image above. As with the first example, if you are using a legacy browser you should see no change in the image -- but you also should not receive a scripting error. If you are using a newer browser that understands external script files, the mouseover image will change (assuming that your browser also supports the images collection) because your browser supersedes the first function with the second.
  • Click on the image above. You will receive an alert that confirms whether or not your browser understands external script files. Browsers that do should execute the function in the external file, while browsers that don't should execute the function contained within the embedded script element.


Benefits:

  • With only slightly more code than required by the first example,this method prevents errors in legacy browsers that do not understand external script files.
  • When the browser is unable to locate the external script file, this method has a beneficial side-effect that prevents script errors even in newer browsers. Because the newer browsers recognize the first &lt;script&gt; element, they will use it in those rare cases when the content of the second &lt;script&gt; element is unavailable to replace that of the first. This is a very powerful benefit.


Drawbacks:

  • None currently known.

Whichever method you choose to use is up to you. Also, if you discover quirks or breakdowns in either of these approaches, please either post the details below or email me at jaylard@members.evolt.org. Happy coding!

What percentage of people???

Submitted by tommy_b on March 17, 2001 - 23:56.

These browsers are ancient!! According to http://websnapshot.mycomputer.com/browsers.html it seems only 0.5% of the browsers on the web might have JavaScript 1.0. Do we really need to bog down the other 99.5% users with all this code? Or slow down the servers if they do conditional page serving?? Sure, I'd love to let everyone beable to access my webpages, but there comes a point where making the server or users suffer for a select few not worth the hassle.

login or register to post comments

what percentage fow who?

Submitted by aardvark on March 18, 2001 - 08:55.

Don't forget, tommy_b, many of us who build sites don't always build sites that conform to the browser stats you see. It's always a function of knowing your audience, not going off someone else's assumptions.

As it is, nobody is getting bogged down with this code. Offloading work to the server is always a better idea, since you can control that environment. Servers are designed to do that kind of work, user's machines aren't always in the best position to throw the CPU cycles at the problem. And when the JS runs on the client as the author proposes, how does that slow that user down any more than writing it the way you would normally write it? I sense your response is based more on the fact that it will take you more time and effort to build it, and that's always a poor excuse. When developers take shortcuts, the users are the ones who suffer.

login or register to post comments

it's not just for old browsers

Submitted by Jeff Howden on March 18, 2001 - 15:23.

another important thing to keep in mind when using external scripts -- there is no way to guarantee that the user will be able to download those external scripts. what happens if they don't? well, their browser is going to behave just like a js1.0 browser that doesn't know what external scripts are. it's going to encounter a block of html with event handlers that refer to a variable or function in that external script and throw an error because the variable or function cannot be found. seems like a nice little safeguard to just drop a few more lines of code in a script block immediately above the script block that references the external script. now you've taken care of not only your "0.5%", but also any number of other situation that modern browsers could encounter.

login or register to post comments

RE: What percentage of people???

Submitted by jaylard on March 19, 2001 - 11:29.

tommy_b, I understand your concern, and would agree if we were talking about extensive additional scripting. However, for the small amount of additional scripting that we are actually talking about, you can make your site compatible with older browsers that support only JavaScript 1.0 while still reaping the benefits of external script files. That's good coding practice, IMO, and a plus on both counts.

Also, with Jeff's approach (example 2 above), there is, as he points out in his post above, the additional benefit of newer browsers gracefully handling those situations where, for whatever reason, an external script file is unavailable. I have to admit (with a little envy ;) that this makes Jeff's approach superior, and the one that I, myself, would likely use. Barring any as-yet-unknown reports of problems with either of these two approaches, however, I think you can't go wrong with providing this small additional support for older browsers.

login or register to post comments

Submitted by Martin Tsachev on June 28, 2001 - 09:14.

It takes advantage of the designed intent of the element: browsers that understand the src attribute should ignore any content contained between the opening and closing tags, while browsers that don't understand it will execute that code while ignoring the external file.

All browsers no matter if they understand the src attribute or not should execute the inline scripts also. See HTML 4 Recommendation

login or register to post comments

Re: HTML 4.01 Recommendation

Submitted by jaylard on June 28, 2001 - 09:28.

Shaggy,

You are mistaken about the src attribute of the &lt;style&gt; element: the HTML 4.01 recommendation plainly states: "If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI."

login or register to post comments

Re: HTML 4.01 Recommendation

Submitted by Martin Tsachev on June 28, 2001 - 10:33.

Sorry about that mistake. I don't quite understand what do you mean with the src attribute of the style element but no matter, everyone makes mistakes.
PS: Didn't post the previous comment twice just connection timeout.

login or register to post comments

Re: HTML 4.01 Recommendation

Submitted by jaylard on June 28, 2001 - 10:54.

Shaggy,

Oops. I meant the <script> element. Sorry for the confusion.

login or register to post comments

we need this!

Submitted by CrashTestDummy on June 1, 2002 - 21:14.

A great article! Many "first generation" web users (usenet junkies, members of societies etc.) started out with IE3 or netscape gold and windows 3.1 and have stuck with what they know! For example, I've just done a site for a group where at least half the users are still using their first home computer. These folks can't browse the web effectively (try even browsing netscape's own site with netscape gold!), they don't like computers much, yet they need to visit their new website. So it really depends on your audience...and these groups really are still out there!

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.