Developer Checklist — JavaScript Appendices

Incorrect code

JS_N_A5: Do not automatically redirect the page using JavaScript

This example shows how not to implement automatic page redirection, using JavaScript during page load to update the location.href. The problem with this functionality is that it breaks the Back button, because pressing Back from the final page will go back to the redirection page, which then triggers the redirection and sends you forwards again.

Some browsers have an extended Back button — usually a small arrow next to the button which gives you a navigable list of recent history. Some browsers even try to remove redirections from the history. But neither of these solutions can be considered sufficient, because they're not common enough or well understood by users.

JavaScript should not be used for automatic page redirection at all, even with user confirmation. Automatic redirection should only be done on the server-side.

Live Demo

The link below will take you to a page containing a large number 1, then automatically redirects to a page with the number 2. If you press the Back button from page 2, you may find it difficult or impossible to press it again and come back to this page, because of the trap created by the redirection on page 1:

Go to Redirection Page 1

JavaScript Code

The JavaScript code for this example can be found in: JS_N_A5.js