spacer

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

home / programming / javascript / domloader To page 1current page
[previous]

DOM-Based Conditional Script Loading in JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

The language attribute is dead--or, as the W3 says, deprecated. None of the pages that use it will validate, whether or not the attribute does anything for the user. And it really wasn't without flaws, to tell the truth. Navigator has some famous problems when you set the language to 1.2--lots of little things don't work the way they ought to. And some browsers will always load the source file, whatever you set the language to. [For more information, see Peter Belesis' thorough examination of conditional script loading for DHTML browsers here - Ed.]

The "legal" way to assign a script to an HTML page is to use the type attribute:

<script type="text/javascript"    
        src="srcfile.js"></script>

Lately most of the code I write is DOM code. IE4 and Netscape, for example, display the plain page, or maybe use some neutral code in a cross browser script. But the biggest part of my code is aimed at DOM enabled browsers, and should have no affect on the others.

The hangnail to this is that even if the browser doesn't set any event handlers or run any procedures, it still has to parse the script file. Even if they are never called, all of the variables and functions are stored in memory. As a quick and dirty solution, the above works, most of the time.

But...

There are some additions to JavaScript that break older versions. Even if you don't call a function from the afflicted client, even if you burrow the new code in ifs and sniffer switches, just parsing a suspect line of code can throw an error. A good example is try...catch error handling. Navigator throws an error when it reads a line that uses try as a method. It is (to N4) a protected keyword. And try is such a nice little method.

This turns a practice that was admittedly inelegant but relatively harmless into an error throwing monster. We are back to where we used to be. We want to only load a script to a browser that can run it.

To Each According to His Ability

If you want to separate the current from the legacy, and give the plain page only to users who can not use the script, you can force the browser to test for a method you want to use, and load a different page if the method is supported.

To add scripting only for the DOM, put this script in the head of the document:

<script type="text/javascript">
   if (document.createTextNode) location.replace('thisforDOMbrowsers.html');
</script>

It works--if a page doesn't support the DOM createTextNode method nothing happens, you stay where you are. If it is supported, the new page will be a copy of the original, with a script included.

This example, however, is rich with difficulties. You may end up with a lot more pages on your Web site. And as long as people can type URLs, it ain't perfect; they can always directly get to a page that will cause problems. Not to mention links and src files that will be twice as hard to maintain. Your users are also apt to complain about being pinballed around your site. And accessibility standards don't like it either.

Gotta be a better way.

The DOM Way

This method solves my problem. It separates browsers that support the DOM from the rest. No support means the script is not loaded. Support means the script is loaded. But it is the same HTML, the same URL, the same page for everyone.

I use a document object method to insert scripts into the head of the document. Instead of this being in the head of the document:

<script type="text/javascript"  src="../../_mr/mrhoo.js"></script>

I use this:

<script type= "text/javascript">
<!--
   if(document.createTextNode){
      var el=document.createElement('script'); 
      el.src='../../_mr/mrhoo.js';
      el.type='text/javascript';
      document.getElementsByTagName('head')[0].appendChild(el);
   }
}
//-->
</script>

Some points to consider about using this loader:

  1. This code doesn't work in N6 versions below 6.1.

    Netscape Versions before 6.1 do not seem to add the contents of the remote script to the global object of the current document. While you can verify that the scripts have loaded by testing the length of the document.getElementsByTagName('script') array, you will not have any way to access the functions from them. All calls to these guys will return undefined.

  2. The code does work in Moz.9+, IE5.5+ and N6.1+.

    Which includes most of our target audience. But the overall point is, the page should still be usable without scripting, anyway. Scripts enhance the pages for the users who can take advantage of it. If they can't use it, they shouldn't miss it.

    I will give N6.01 and below the plain page, same as IE4 and Opera and the rest of the browsers that don't support the createTextNode method.

    To each according to his ability.

# # #

About the Author

Kenneth Tibbetts is the creator of the Yankee Web Shop at http://www.yankeeweb.com/webshop.html and can be reached at: postmaster@yankeeweb.com.


home / programming / javascript / domloader To page 1current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

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

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