Saturday, 21 March 2009

JavaScript support levels

Generally, i breakdown JavaScript support into 4 levels. This makes life much easier and as i'm a lazy bas****d it suits me to keep development time down to a minimum.

I'm using something like this...


window.user = {
jsSupportLevel : 0,
init: function(){
if(!document.getElementById){return;}
var ua=navigator.userAgent.toLowerCase();
var platform=navigator.platform.toLowerCase();
var isPC = (platform.match('win32')||platform.match('win64'))?true:false;
var isDeskTop = ( ua.match('firefox') || isPC || platform.match('macintel') )?true:false;
var support = 1;
if (window.XMLHttpRequest){support = 2}
if ( support==2 && isDeskTop ){
support = 3;
}
this.jsSupportLevel = support;
}
};user.init();


This will give you a js global "user" and you use it in your js to determine whether to do some enhancements or not.
e.g.
if (user.jsSupportLevel>1){
... attach Ajax related event handlers ...
}


Example support levels

0 = user-agent doesn't understand DOM (!document.getElementById)
1 = user-agent DOM enabled
2 = user-agent has native ajax support (window.XMLHttpRequest)
3 = user-agent is probably a desktop browser

You can use the above to deliver the appropriate level of JavaScript support to users, e.g. Don't implement any JavaScript if the support level is 0, only do very basic JavaScript for level 1, do pretty much everything for level 2 and level 3 is level 2 with transitions and opacity (stuff you wouldn't want on a mobile/hand-held).

This will only work if your using progressive enhancement of course!

2 comments:

  1. How do you approach the design phase for multiple experiences? Or is it more ad-hoc than that?

    ReplyDelete
  2. With some difficulty, I am hoping to get the design phase to reflect progressive enhancement, e.g. a page design would be based on the no-script basic behaviour with notes/elaborations on dynamic content.
    However, this has been an up hill struggle as designers don't have time to change they way they think!
    So, the current work-a-round is to have a set of defined 'behaviours' e.g. if a new link is created which on clicking imports some content into the current page (the no-script behaviour for the same link is that it leads the user to a page containing the required content) then this link can be marked in the design as using behaviour X so it can be tested along with all the other links which share behaviour X, if that makes sense?

    ReplyDelete

 

Note: the opinions on this page don't represent that of my employer in anyway. And, actually only vaguely represent my opinions on the date of publication. (Unlike Ms T, I am very much for turning)