spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / domloader current pageTo page 2
[next]

Value Added Scripts

Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies

DOM-Based Conditional Script Loading in JavaScript

By Kenneth Tibbetts (postmaster@yankeeweb.com)

It can be tough to be practical and ethical at the same time. Ethically, I believe in the W3C recommendations. I believe in advancing and supporting accessibility and the document object model (DOM). The Internet should exclude the fewest possible people. But practically speaking, I write code. And the code that I write usually requires a modern, document object supportive browser to really do its stuff.

My pages begin life as ordinary HTML and CSS. When I add scripts to these clean, valid pages, I wind up with something everyone can use, to some extent. If I am very, very careful.

It is not too difficult to write pages that work in either and both of these cases:

  1. Browsers (and other software) without script support, or scripting turned off.
  2. Browsers that support the latest version of the JavaScript language.

It is fairly easy to write scripts that work in the newest browsers. The trouble starts when the real world gets online, with browsers that support some but not all of the script. Give a half-smart browser enough code and it'll hang itself.

I run a small shop, with a small budget. I don't have a test machine for every platform. I want to deliver my scripts only to the visitors that can use them.

Dead Language

If you set up your pages to run basic HTML, and limit the built in events to hyperlink navigation and server based forms, any page can display in any browser. Any other events can be built in scripts, and you can restrict the visitors who load the scripts to the folks who can use them. To each according to his ability.

In HTML, before the W3 put the kibosh on the language attribute, you could screen the browsers by setting a version number of the language in the script tag:

<script language="JavaScript1.5" src="srcfile.js"></script>

This would prevent a browser that didn't support version1.5 from loading the file.

You could get fancier, and load different scripts for various levels of support:

<script language="JavaScript"    src="srcfile1.js"></script>
<script language="JavaScript1.2" src="srcfile2.js"></script>
<script language="JavaScript1.5" src="srcfile3.js"></script>

Scripts here are cumulative. The first script loads in any browser that supports the src attribute for external scripts. If number 3 loads, you can count on having the first two as well. Each can have an onload handler--the last handler assigned will be used when everything finally loads.

I wrote a lot of code like this, back when. But no more.


home / programming / javascript / domloader current pageTo page 2
[next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji

Created: April 30, 2002
Revised: April 30, 2002

URL: http://webreference.com/programming/javascript/domloader/