1. Home
  2. Computing & Technology
  3. JavaScript

Accessibility and JavaScript
Separate Pages

By Stephen Chapman, About.com

One option for making your site accessible to all of your visitors is to create one set of pages for those with JavaScript and a second set of pages for those without. This is definitely not the best solution to this accessibility issue but it is the one that is the easiest inn terms of the code that you need to set it up and get people onto the right version of the page.

The way that we set this up is to set up the link to the page we want those without JavaScript to see in our HTML.

<a href="nonjs1.htm" id="js1">Link to next page</a>

The only thing in the way that this is coded that makes it any different from a regular link to another page is that we have given the link an id so that we can reference this link from our JavaScript and override where it will take our JavaScript enabled visitors.

loaded('js1','js1');
function js1() {document.getElementById('js1').onclick = function() {location.href = 'js1.htm'; return false;}}

This JavaScript contains the command to redirect our visitor to the JavaScript enabled version of the web page and then tells the browser to disregard the link to the non JavaScript version of the page. Which of the two pages that our visitor ends up on will therefore depend on whether their browser has JavaScript enabled or not and the selection of which of the two pages to display will be decided by whether the browser is able to run the JavaScript or not.

In fact this code is a little more selective than that. Inside of the loaded() function we have the following line of code:

if (document.getElementById && document.getElementById(i) != null) f();

This code uses what is called feature sensing to check if the browser supports using document.getElementById and also if it dies it then tests if there is an id in the page with the specified value. Only if both of these conditions are true does the function to add our JavaScript code to the link actually get run.

What this means is that when someone is using an old browser that doesn’t support the current JavaScript standards that their browser will not try to run the code that it doesn’t understand but will instead treat the browser as one which does not support modern JavaScript and the non JavaScript version of the page will therefore be the one that those visitors see. This will save us from cluttering our code with alternative code that is specific to really old browsers that almost no one is using any more which could potentially triple the amount of code that we would need to write and where the additional code would only be used by a very small and falling number of our visitors. In effect we just treat those really old browsers that don’t support the current standards (those browser versions earlier than IE5 and Netscape 6) as if they don’t support JavaScript (which in so far as much of the modern standards and what can be done with JavaScript today – they don’t).

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Separate Pages>

©2009 About.com, a part of The New York Times Company.

All rights reserved.