Friday, December 21st, 2007

Dean Edwards releases base2 and base2.DOM beta

Category: JavaScript, Library

<p>Dean Edwards has put base2 and base2.DOM out in beta:

From now on base2 will be served from a permanent URL on googlecode. The URL will reflect the version number of the library. You can find the current (beta) libraries here.

base2.DOM has had a few months to mature now and there are no outstanding defects logged at googlecode. I’ll keep the code in beta for a month or two before issuing a final release. The API remains pretty much the same as when I first released base2.DOM. I have added some standards support to the style object but that’s about all.

I’m also releasing the core library, base2. I’ll blog more about this library sometime in the future. In the meantime you can browse the API and read the skeletal documentation

Dean wrote up an introduction to base2 where he covers the problems that he is trying to solve:

  • Inconsistent enumeration across platforms
  • instanceof doesn’t always work
  • typeof is inconsistent (RegExps identify as type “function” in Mozilla browsers)
  • Consistent object/platform detection
  • Other various bugs

And the sugar:

Take a bit of time to look at the code as it is a great way to learn.

I like this little ditty:

javascript
< view plain text >
  1. detect: new function(_) {  
  2.     // Two types of detection:
  3.     //  1. Object detection
  4.     //    e.g. detect("(java)");
  5.     //    e.g. detect("!(document.addEventListener)");
  6.     //  2. Platform detection (browser sniffing)
  7.     //    e.g. detect("MSIE");
  8.     //    e.g. detect("MSIE|opera");
  9.        
  10.     var global = _;
  11.     var jscript = NaN/*@cc_on||@_jscript_version@*/; // http://dean.edwards.name/weblog/2007/03/sniff/#comment85164
  12.     var java = _.java ? true : false;
  13.     if (_.navigator) {
  14.       var element = document.createElement("span");
  15.       // Close up the space between name and version number.
  16.       //  e.g. MSIE 6 -> MSIE6
  17.       var userAgent = navigator.platform + " " + navigator.userAgent.replace(/([a-z])[\s\/](\d)/gi, "$1$2");
  18.       // Fix opera's (and others) user agent string.
  19.       if (!jscript) userAgent = userAgent.replace(/MSIE[\d.]+/, "");
  20.       java &= navigator.javaEnabled();
  21.     }
  22.    
  23.     return function(expression) {
  24.       var r = false;
  25.       var not = expression.charAt(0) == "!";
  26.       if (not) expression = expression.slice(1);
  27.       if (expression.charAt(0) == "(") {
  28.         // Object detection.
  29.         try {
  30.           eval("r=!!" + expression);
  31.         } catch (error) {
  32.           // the test failed
  33.         }
  34.       } else {
  35.         // Browser sniffing.
  36.         r = new RegExp("(" + expression + ")", "i").test(userAgent);
  37.       }
  38.       return !!(not ^ r);
  39.     };
  40.   }(this)

Related Content:

Posted by Dion Almaer at 6:01 am
2 Comments

++++-
4 rating from 22 votes

2 Comments »

Comments feed TrackBack URI

var element = document.createElement(“span”); ???

Comment by FredCK — December 21, 2007

@Dion: Here’s the documentation for the detect() method that you like:

http://base2.googlecode.com/svn/doc/base2.html#/doc/!base2.detect

Comment by deanedwards — December 21, 2007

Leave a comment

You must be logged in to post a comment.