Common accessibility practice has said that JavaScript URIs should not be used. For example, <a href="javascript:doSomething();" >Action Link</a>, will do nothing if JavaScript is not available.   Now that WCAG 2.0 is providing for baselines that assume JavaScript is available and enabled, a discussion of JavaScript URIs and invoking JavaScript from an anchor link becomes relevant.   While the DHTML Roadmap technologies remove the need to use anchor tags for keyboard support in IE 5.5+ and Firefox 1.5, older browsers still require this.  In in order to build complex, keyboard accessible JavaScript widgets, developers are going to rely on anchor tags to call a JavaScript method.  I'd like to start a thread to discuss the pros and cons of JavaScript URIs and calling JavaScript functions from an anchor tag.  

Assume doSomething() is a JavaScript function that may or may not take any parameters.  Below are some techniques for creating links to invoke a JavaScript function.  In these examples, assume loading another page is not an option.
 
<a href="javascript:doSomething();">Action Link</a>
<a href="javascript:;" onclick="doSomthing();">Action Link</a>
<a href="javascript:doSomething();" onclick="doSomthing();return false;">Action Link</a>
<a href="#" onclick="doSomething();return false;">Action Link</a>
<a href="/" onclick="doSomthing();return false;"><Action Link</a>

Are there other methods? Is one recommended over others?  What are the pros and cons? What about the effect on the browser history?  While a javascript: URI is not defined in any official specification, I've attached a simple test file using these methods that passes validation as XHTML 1.0 transitional.



thanks for your input,

Becky Gibson
Web Accessibility Architect

                                                     

IBM
Emerging Internet Technologies
5 Technology Park Drive
Westford, MA 01886
Voice: 978 399-6101; t/l 333-6101
Email:
gibsonb@us.ibm.com