Skip to main content

Scripts are keyboard accessible. If the content affected by scripting is not accessible, an alternative is provided.

On this page:



Rationale

Scripting, including JavaScript and the standard version ECMAScript, enables Web developers to enhance sites with dynamic content, visual effects, user interaction, and client-side processing. Scripts can update HTML content and perform visual enhancements to a Web site. Scripts can respond to many types of manual and automatic events. Some of these events include: the loading of a Web page, the user selecting an item in a menu, and the submitting of a form. Due to the virtually limitless ability of scripts to affect most any content, discussing the techniques for making the use of JavaScript accessible is complex and sometimes challenging.

The general approach to making scripting accessible is to ensure the functionality of the scripts is accessible from the keyboard, and to ensure the information (content) is available to assistive technologies.

Scripts that provide both of these features are considered directly accessible. If one or both of the above cannot be achieved, then additional techniques are used to reduce the need for scripting, or to provide equivalent content that does not rely on script functionality. This checkpoint provides recommendations and techniques for accessible scripting.

Required development techniques

Compliance with this checkpoint requires all of the following techniques be met. These techniques are defined in the WCAG 2.0 Level A Success Criterion for Guideline 2.1.1 (link resides outside of ibm.com).

1. Functions: Use both keyboard and other device-specific functions.

2. JavaScript actions: Make JavaScript actions keyboard accessible.

Examples for Script developers

1. Functions: Use both keyboard and other device-specific functions.

To comply with this technique, all of the following examples must be implemented.

Example 1

In the following example, the developer wants additional text to be displayed when a user with a mouse puts the cursor over the image (onMouseOver). To make this type of functionality accessible, onFocus and onBlur must also be used. In addition, because this is essentially an "image-swap", alt text must be added to the image, so that visually impaired users have access to the content as well. The image-swap below can be executed with a mouse or with a keyboard.

Accessibility - The quality of being accessible, or of admitting approach; receptibility.

The code for the above example is below:


<a href="webscripts_eventhandlers.html#imageswap" onmouseover="document.images['ex1pic2'].src = ex1on.gif';"
onfocus="document.images['ex1pic2'].src = 'ex1on.gif';"
onmouseout="document.images['ex1pic2'].src = 'ex1off.gif';"
onblur="document.images['ex1pic2'].src = 'ex1off.gif';"><img src="ex1off.gif" width="289" height="79" id="ex1pic2"
alt="Accessibility - The quality of being accessible, or of admitting approach; receptibility." /></a>


The following table suggests keyboard event handlers to pair with mouse event handlers.

Mapping of event handlers for the mouse to the keyboard
Use... ...with
mousedown keydown
mouseup keyup
click [1] keypress [2]
mouseover focus
mouseout blur

Example 2

Implement WAI-ARIA keyboard navigation.

Note that WAI-ARIA examples are not supported by all browsers. If your product needs to be accessible in multiple browsers one or more of the required HTML/CSS specific techniques must be implemented as well as a WAI-ARIA technique.

Rich Internet application keyboard navigation should behave like desktop application keyboard navigation. For example, keyboard users tab to Windows Explorer and then use the arrow keys to navigate to elements in the tree. WAI-ARIA provides support for arrow key navigation of complex widgets such as menus, trees and grids via the tabindex attribute or activedescendant property. For more information on how to implement arrow key navigation, see WAI-ARIA Best Practices (link resides outside of ibm.com) and Keyboard navigable JavaScript widgets (link resides outside of ibm.com).

For additional information, refer to WCAG 2.0 examples for using both keyboard and other device-specific functions (link resides outside of ibm.com).

2. JavaScript actions: Make JavaScript actions keyboard accessible.

To comply with this technique, all of the following examples must be implemented.

Example 3

The keyboard tab key must be able to be used to "stop" at the element and then activate it by pressing the Enter key.

Typically only links and form controls are in the keyboard tab order. However, complex widgets such as trees, menus and grids should also receive focus via tabbing, and arrow key navigation should be supported for navigating elements within these widgets. For more information on how to implement arrow key navigation for complex widgets, see WAI-ARIA Best Practices (link resides outside of ibm.com) and Keyboard navigable JavaScript widgets (link resides outside of ibm.com).

In addition, browsers will sometimes map the pressing of the keyboard Enter key to mouse events. Therefore, device-dependent event triggers must be tested to determine if the browser simulates the event with the keyboard as a redundant input mechanism. If it does, then specifying the mouse event is sufficient. Two events might cause a problem when only one is expected. For example, onKeyDown should not be specified with onMouseDown because doing so will cause two browser events: one for the onKeyDown event because the user pressed down the Enter key and a simulated mouse click provided by the browser for onMouseDown.

If the browser does not simulate the mouse event when the keyboard is used, then the keyboard event must also be specified.

OnClick is an example of an event handler that sometimes behaves as a device-independent handler and in other cases as a device dependent handler. Specifying the event handler onClick on a link or form element should cause the browser to evoke the event if either the mouse or the Enter key is used to select it. In this case, onClick acts in a device-independent manner, and is therefore directly accessible because the same action takes place when the link is clicked or the enter key is pressed. In other words, for a link that uses onClick, if tabbing to a link and pressing the Enter key will cause the onClick event to occur - then the script functionality can be operated via the keyboard without requiring the developer to code the second redundant onKeyPress event for the same element.

The example below uses onClick as a device-independent link, which is executed when the Enter Key is used to select the element:

View this device-independent onClick example

The code for the Example 1 is below:


<a href="webscripts.html#postexample" onclick="return confirm('Clicking OK will take you to the next paragraph');">View this device-independent onClick example</a>

When the onClick event is used with elements that are not links or form controls, onClick is device-dependent, therefore you must provide a redundant device-independent event handler (onKeyPress) or an equivalent alternative. In order to gain focus on the text, you will need to implement the tabindex attribute. Once that is done, you can use the onkeypress event handler to provide an alternative to onClick. In Example 2 below, the "View this onClick with onKeyPress example" text has the tabindex set, and has both the onClick and onKeyPress event handlers defined.

Note complex widgets such as trees, menus and grids have a different navigation paradigm. Complex widgets should receive focus via tabbing, and arrow key navigation should be supported for navigating elements within the widgets. For more information on how to implement arrow key navigation for complex widgets, see WAI-ARIA Best Practices (link resides outside of ibm.com) and Key-navigable custom JavaScript widgets (link resides outside of ibm.com).

Example 4

Do not write event handlers that use double-clicking (onDblClick).

There is no keyboard equivalent specified in HTML 4.01. Instead, use one of the keyboard accessible handlers.

Example 5

Do not write event handlers that rely on mouse coordinates.

There is no keyboard equivalent way to perform this function. An example of a script that relies on mouse coordinates is a server-side image map which uses javascript:onmouseover as a precursor to performing an action.



Required test techniques

The following test tools and techniques are required to test this checkpoint.

  1. Test tools
  2. Required accessibility verification test techniques

Test tools:

Install the following tools to test this checkpoint:

Required accessibility verification test techniques:

Use the following accessibility verification test (AVT) techniques to validate the Web content. It is recommended that these tests be performed in order.

1. Web syntax analyzer test
  Action Result
Test the Web site with a Web syntax analyzer to verify the compliance criteria as follows:

The tool does not identify any of the following
Pass:
Fail:
2. Manual test:
  Action Result
Verify the compliance criteria for this technique as follows.
Pass:
Fail:
3. Screen reader test:
  Action Result
Verify the following compliance criteria with a screen reader. For detailed instructions on how to test this checkpoint with a screen reader follow the 'Screen reader accessibility verification tests for Web checklist 5.1' document.
Pass:
Fail:

®2009 IBM Corporation

Last updated December 07, 2009.

W3C Recommendation 11 December 2008: http://www.w3.org/TR/WCAG20/ (link resides outside of ibm.com)
Copyright 1994-2009 W3C (Massachusetts Institute of Technology, European Research Consortium for Informatics and Mathematics, Keio University), All Rights Reserved.