Summary
I posted a server-side solution to allow users to define their own access keys using a PHP class. This is a classic ASP version of the same class, including an example of how to use the class.
Author: Gez Lemon
Contents
The AccessKey Class
The ASP AccessKey
class allows users to define their own access keys. Access keys are a contentious area of accessibility, as the way they have been implemented in some user agents mean that they clash with the shortcut keys of the browser. The class provides various methods to help developers provide their own customisation form, along with methods to use them in their markup. For a PHP version of this class, please see the PHP version of this article.
The file akasp.zip contains the class and a sample file illustrating how to use the class. The following is an example of the markup to create the user-defined access key form.
<% Option Explicit %>
<!-- #include virtual="/classes/ak.class.asp" -->
<%
Dim objAK
Set objAK = New AccessKeys
objAK.addAccessitem "Home", _
"/index.asp", _
"1"
objAK.addAccessitem "Search Criteria", _
"/contact.asp", _
"4"
objAK.addAccessitem "Quality Assurance", _
"/services.asp", _
Null
objAK.addAccessitem "Articles Archive", _
"/articles.asp", _
Null
If Request.Form("submit") <> "" Or _
Request.Form("default") <> "" Then
If Request.Form("default") <> "" Then
objAK.useDefaults
Else
objAK.setCookies
End If
End If
%>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-gb">
<head>
<title>User-Defined Access Keys</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>User-Defined Access Keys</h1>
<%
objAK.displayOptions "nav"
If Not objAK.isValid Then
Response.Write objAK.getErrors
End If
%>
<form id="accesskeysetup" action="ak.asp" method="post">
<% objAK.generateForm False %>
<p>
<input type="submit" name="submit"
value="Assign Accesskey Bindings">
<input type="submit" name="default"
value="Assign Default Bindings">
</p>
</form>
</body>
</html>
<%
Set objAK = Nothing
%>
AccessKey Methods
The following lists the methods that can be used with the AccessKeys
Class.
addAccessitem Method
The addAccessitem
method allows a single item to be added to the collection of items that may be assigned an accesskey
.
- @parameter String
strItem
(The name of the item to be added to the collection) - @parameter String
strValue
(The value for the item) - @parameter String
strDefault
(The suggested value for the access key, or Null for no suggestion)
Examples
objAK.addAccessitem "Meat", "meat.asp", Null
objAK.addAccessitem "Fish", "fish.asp, "9"
removeAccessItem Method
The removeAccessitem
method removes an item from the collection of items that may be assigned an accesskey
.
- @parameter String
strItem
(The name of the item to be removed from the collection)
Example
objAK.removeAccessitem "Meat"
isValid Method
The isValid
method returns a Boolean value indicating whether or not the values assigned were valid.
- @parameter none
Example
If Not objAK.isValid Then
Response.Write objAK.getErrors
End If
getErrors Method
The getErrors
method returns a string containing errors from form submissions, or an empty string if there are no errors. The getErrors
method should be used with the isValid
method.
- @parameter none
Example
If Not objAK.isValid Then
Response.Write objAK.getErrors
End If
setCookies Method
The setCookies
method returns a Boolean value indicating whether or not the values were valid accesskey
values. If the values are inappropriate, isValid
returns False
, and getErrors
can be used to retrieve the errors.
- @parameter none
Example
objAK.setCookies
removeAllBindings Method
The removeAllBindings
method removes all access keys defined for the collection.
- @parameter none
Example
objAK.removeAllBindings
displayOptions Method
The displayOptions
method displays a definition list where the definition terms are the names in the collection, and the definitions contain the assigned key, and suggested key if available.
- @parameter String
strID
(optional parameter to assign anid
attribute to the definition list,Null
if no parameter to be used)
Examples
Display options without specifying an id
.
objAK.displayOptions Null
Display options with an id
.
objAK.displayOptions "someid"
generateForm Method
The generateForm
method generates form sections based on the names provided in the collection. The method takes a parameter to indicate whether the generated markup is XHTML.
- @parameter Boolean
bXHTML
(Indicates markup format;True
= XHTML,False
= HTML)
Examples
The following generates the form markup for HTML:
objAK.generateForm False
The next example generates the form markup for XHTML:
objAK.generateForm True
useDefaults Method
The useDefaults
method assigns all of the values that have been suggested as default keys by the author, if any have been provided.
- @parameter none
Example
objAK.useDefaults
getAccessitem Method
The getAccessitem
method returns the markup for a particular item, including the accesskey
if one has been defined. If the item doesn't exist in the collection, the method returns an empty string. The accesskey can be ignored using the bKey
parameter, in which case, just the markup will be returned. As access keys may be assigned to links, labels, or form controls, a parameter allows the markup to return label
tags or link tags.
- @parameter String
strItem
(The item to be returned) - @parameter Boolean
bKey
(Indicates whether the accesskey value should be included) - @parameter String
strElement
(Element to be returned; eitherlink
orlabel
)
Examples
An example of how this might be used in a navigation list
<ul id="navigation">
<li>
<%= objAK.getAccessitem "Home", True, "link" %>
</li>
<li>
<%= objAK.getAccessitem "Contact Us", True, "link" %>
</li>
</ul>
An example of how this might be used with a form element.
<%= objAK.getAccessitem "Search", True, "label" %>
<input type="text" id="search" name="search" size="10">
The following retrieves a link without the access key assignment.
<%= objAK.getAccessitem "Contact Us", False, "link" %>
Category: Accessibility.
[user-defined-access-keys-aspversion.php#comment1]
Very nice Gez,
My approach is not as elegant as yours, but may help ASP beginners:
http://www.tjkdesign.com/articles/user_defined_accesskeys.asp
Posted by Thierry Koblentz on
[user-defined-access-keys-aspversion.php#comment2]
Thank you, Thierry, and thank you for posting your version. I would like to see accesskeys used more, and empowering users by allowing them to set their own definitely seems to be the right approach to me.
Nice work.
Posted by Gez on