Tutorial Menu

Basic AJAX - Best Practice

The previous examples may give a user a chance of working but without careful thought it can create confusion for other assistive technologies. AJAX can be used to help aid users in the use of a website. Webcredible state that the following should be incorporated:

(webcredible, 2008)

If we apply this to the previous ideas discussed then the functionality of setting the focus or attempting to cause the virtual buffer on a screen reader to refresh is deemed confusing to a user. Instead the article suggests that the following changes are made. First start with the basic AJAX class as there is no functionality to set the focus or update the screen reader.

First update the AJAX class to incorporate the following standard loading message:

onSend:function(){
  var loadDiv=document.createElement('div');
  loadDiv.setAttribute('id','load');
  var loadText=document.createElement('p');
  loadText.appendChild(document.createTextNode('Loading more content'));
  loadDiv.appendChild(loadText);
  var objContent = document.getElementById('content');
  objContent.appendChild(loadDiv);		
}, 
onLoad:function(){
  var loadDiv=document.getElementById('load');
  document.getElementById('content').removeChild(loadDiv);
}

This is useful as there is no need to put a loading div in the document body.

Next add in the init functionality previously discussed.

window.onload = init;
function init(){
  giveOptions();
  if (get_cookie ( 'isAJAX' )||!document.getElementById || 
      !document.createTextNode || (typeof XMLHttpRequest == 'undefined' 
        && !ActiveXObject)){
    return;
  }
  createListeners();
}
function createListeners(){
  var objAnchor = document.getElementById('link');
  if (objAnchor){
    objAnchor.onclick = function(event){return makeCall();};
    objAnchor.onkeypress = function(event){return makeCall();};
  }
}
function removeListeners(){
  var objAnchor = document.getElementById('link');
  if (objAnchor){
    objAnchor.onclick = function(event){return true;};
    objAnchor.onkeypress = function(event){return true;};
  }
}
function giveOptions(){
  var accesslist=document.getElementById('accessMenu');
  if(accesslist){
    var newOpt=createLink();
    var listItem=document.createElement('li');
    listItem.appendChild(newOpt);
    accesslist.appendChild(listItem);
  }
}
function createLink(){
  var newOpt=document.createElement('a');
  newOpt.setAttribute('href' , '#');
  newOpt.setAttribute('id' , 'AJAXchoice');
  newOpt.setAttribute('href' , 'javascript:setAJAX()')
  if(get_cookie ( 'isAJAX' )){
    newOpt.appendChild(document.createTextNode(('Turn dynamic content on'));
  }else{
    newOpt.appendChild(document.createTextNode('Turn dynamic content off'));
  }
  return newOpt;
}
function setAJAX(){
  if(get_cookie ( 'isAJAX' )){
    delete_cookie ( 'isAJAX' );
    var newOpt=createLink();
    var AJAXLink=document.getElementById('AJAXchoice');
    AJAXLink.parentNode.replaceChild(newOpt,AJAXLink);
    createListeners();
    alert("Dynamic content is enabled on this site");
  }else{
    set_cookie('isAJAX', 1);
    var newOpt=createLink();
    var AJAXLink=document.getElementById('AJAXchoice');
    AJAXLink.parentNode.replaceChild(newOpt,AJAXLink);
    removeListeners();
    alert("Dynamic content is disabled on this site");
  }
}

The only change is to replace the information to 'Turn dynamic content off' as this is simpler for someone who does not know the term AJAX to understand. This will also inform the user that there is dynamic content.

The second change is to highlight the changed text. This is done through setting the background colour of the text. The colour can fit into your websites colour scheme. Inb the example I chose yellow by placing the following line of code in the makeCall function.

document.getElementById('dataTarget').style.backgroundColor='yellow';

See example

This option enables the majority of users the opportunity to user the web page. The dynamic content can be turned on and off alongside highlighting page changes and it works with JavaScript disabled.

Previous: Basic AJAX - Incorporating alternatives Next: Common uses

References

Webcredible. (2008). AJAX accessibility for websites. Retrieved 2008, march 12 from webcredible.com: http://www.webcredible.co.uk/user-friendly-resources/web-accessibility/ajax-accessibility.shtml

Elated Communications Ltd. (2002). Tutorial: JavaScript and cookies. Retrieved January 18, 2008, from elated.com: http://www.elated.com/articles/javascript-and-cookies/

Lemon, G., Faulkner, S. (2006, May 25) Making Ajax Work with Screen Readers. Retrieved March 31, 2008, from http://juicystudio.com/article/making-ajax-work-with-screen-readers.php

Lemon, G., Faulkner, S. (2007, January 19) Improving Ajax applications for JAWS users. Retrieved March 31, 2008, from http://juicystudio.com/article/improving-ajax-applications-for-jaws-users.php

Eichorn, J. (2006). Understanding AJAX. Upper Saddle River, NJ: Pearson Education, Inc.

Zakas, N. C. (2005). Proffessional JavaScript for web developers. Indianapolis: Wiley Publishing Inc.