Monday, 19 May 2014

I Don’t Trust Browsers with my JavaScript

Recently, at the jQuery UK 2014 #jquk convention Adam J Sontag @ajpiano of the jQuery Foundation gave a State of jQuery talk which was great, and he told us about the virtues of jQuery of which there are many, but I felt that he skipped what I now feel is the most important aspect of jQuery (or he didn’t and I just brain farted).

Browsers! Not that I don’t like them. In fact I love them. Today the internet browser, that window on the World Wide Web is a sophisticated bit of kit. It is cutting edge, fast, multi-purpose and generally, well, awesome. We ask so much of it and it provides, and they have done so for many years. We may laugh now at IE6 but it was hot stuff when it first came out, but here is the problem – they are all fighting to be the best, the fastest and the coolest, and in so doing, they never stop to take stock. I am not saying this is a bad thing. It is a good thing that the browser market is so competitive. We the end users get an incredible product for free. The latest browsers, IE included are truly incredible, but you cannot trust them.

As a web designer and developer for more years than I care to count, I have seen features come and go. They light up the scene for a bit and then they are made obsolete. What happens to all that code that uses the now obsolete feature? It has to be trashed. Element behaviours are a perfect example. They were an IE only feature that were way ahead of their time, but if you used them, now you have to re-write them.

I suppose things are getting better when it comes to JavaScript, but for years JavaScript features came and went, especially in IE but also the other browsers starting with Netscape. We had to write hacks for different browsers and check for versions and duplicate this and fall back to that. 

Then libraries like jQuery came along and kind of fixed it. Just like that we could write code once and it would work cross browser. Nice! It fixed this by being a layer between your code and the DOM. Your code stays the same while any browser specific changes happen in the jQuery layer.

Today there is a lot of talk on the subject of the need for jQuery. Do we still need it now that browsers are so compliant with JavaScript standards? Perhaps not (we do), but fool me once…

I am not ready to make the mistake twice - of trusting browsers, or at least not just yet. The point of jQuery is not so much the nice way it lets you play with the DOM. The point is the abstraction we make between the code and the engine. Forget jQuery for a second and let me just say this: Put a layer between your code and the JavaScript engine and you will only ever have to fix your code in one place should a browser suddenly decide it wants to deprecate or change a feature.

If you agree that this is a good idea, then why not use a ready made layer? jQuery is such a layer and it was made, and is kept up to date by some very clever people. You see? You might as well use jQuery. If worst comes to worst and jQuery suddenly becomes redundant for some reason, but browsers keep changing, you just need to change your mindset by one degree and see jQuery as a layer that you own and you can modify.

I guess all I am doing is arguing for jQuery and felt that the most important aspect was not highlighted enough. I should have said so at the convention. Now I am making up for it. Don’t make the mistake others have made. Abstract your code, and whilst doing that you may as well get a head start with jQuery.

No comments:

Post a Comment