Click here to Skip to main content
15,881,172 members
Articles / Web Development / HTML
Article

AJAX but accessible links

Rate me:
Please Sign up or sign in to vote.
2.71/5 (5 votes)
15 Nov 20061 min read 27.7K   140   7   6
A JavaScript tip to make on the same HTML link a JavaScript call or a classic navigate for a new tab/ new window/ disabled JavaScript tool.

Introduction

You want to make a website with an AJAX-based navigation to only refresh changes that show the right content? Cool! It will bring a very reactive website.

<a href="javascript:LoadPage1()">page1</a>
a link calling a javascript method to ajaxify your page

Problem 1: This approch prevents your users from navigating in your website if they have disabled JavaScript.

Problem 2: It also prevents your users from opening this link in a new window or in a new tab :'(

Philosophy of "AJAXed and accessible links"

I will give you a very small and simple implemantation of my links approach which:

  • Allows the user to use AJAX on a simple click in the links
  • Make a classic navigation (using the href) when the user does a simple click without JavaScript enabled in his browser
  • Make a classic navigation if the user wants to open the link in a new tab or a new window

I choose the following implementation to decide to use a classic navigation if the user clicks on the link with:

  • the middle button
  • CTRL key pressed
  • SHIFT key pressed

In your website design, you have to make all your pages accessible by a link, redering the right state of the page on the server.

Implementation

I include a JavaScript SmartLink function in the source file you can download from above. To use it, you have to write your links like that:

HTML
<a id="SmartLink1" href="http://www.codeproject.com" 
       onmousedown="SmartLink(event, this, 'MyAjaxLink1()');">
  my smart link 1
</a>

The href attribute will be used if JavaScript is disabled, or for a new window/new tab,
the 'MyAjaxLink1()' will be the JavaScript call to do for the AJAX-ified link functionnality.

Conclusion

This approach needs to be more packaged but it will bring you a solution to make, at the same time, a disabled-JavaScript complient website and a cool AJAX website without breaking user friendly options like new tab/new window browsing.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReturn False Pin
jammer122114-Jan-07 13:10
jammer122114-Jan-07 13:10 
GeneralSearch Engine Friendly Pin
Jcmorin15-Nov-06 6:09
Jcmorin15-Nov-06 6:09 
It also solve the search engine crawling issue. Bots do not run Javascript and simulate mouse click to navigate your web site!
GeneralRe: Search Engine Friendly Pin
seubi15-Nov-06 6:25
seubi15-Nov-06 6:25 
GeneralWorks but be careful Pin
robrich15-Nov-06 6:08
robrich15-Nov-06 6:08 
GeneralRe: Works but be careful [modified] Pin
seubi15-Nov-06 6:23
seubi15-Nov-06 6:23 
GeneralGood concept Pin
Shawn Poulson15-Nov-06 3:31
Shawn Poulson15-Nov-06 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.