var $ = document.querySelectorAll.bind(document);
Element.prototype.on = Element.prototype.addEventListener;
$('#somelink')[0].on('touchstart', handleTouch);
Beautifully simple.
I’ve taken this idea a little further and used it on a few specific projects, adding support for chaining, looping and simplifying the syntax. Comes in at <200 bytes gzipped. But the point is that today we have features natively available, and I’m trying to consider my audience before dropping in jQuery by default.
The BBC use the following test for cutting the mustard:
if ('querySelector' in document &&
'localStorage' in window &&
'addEventListener' in window) {
// bootstrap the JavaScript application
}
I know off the top of my head IE8 doesn’t supportaddEventListener(but there is a polyfill)
$('body').addClass('hasJS');
or
pure javascript:
document.body.className = 'hasJS';
If the body might have a class already, just append (jQuery has to read the className property too):document.body.className += ' hasJS'.
