Strict Mode in EcmaScript 5

EcmaScript 262 rev. 5 introduced a “strict mode”1, which gives you much needed, stricter parsing (and removes some of the bad parts know from rev. 3).

InfoQ has a good introdction to how strict mode is intended to work

Firefox nightly2 (tested in 3.7a2pre) now has support for strict mode, which means I will now get early warnings of silly mistakes, long before I even run JSLint on my code.

 1 // use of unscoped or undeclared variables will generate exceptions
 2 function myFlawedFunction(){
 3   i = 3; // generates an exception
 4   return i;
 5 }
 6 
 7 function myBetterFunction(){
 8   var i = 3; // does not generate and exception
 9   return i;
10 }

To get the full benefit of strict mode without causing too many headaches of having library code evaluated in strict mode, you can wrap your source in an immediate function.

1 // wrap the source in an immediate function 
2 // to limit strict mode to only this scope
3 (function(){
4   "use strict";
5   // your code here        
6 }());

I’ve created a simple test to show if your browser supports strict mode, why not go test it now?.

As far as I’ve been able to determine, none of Firefox’s competitors nightly builds have support for strict mode.

Firefox nightly has just become my new favorite browser for development.

1 John Resig explains strict mode and more

2 Get a Firefox nightly build and start enjoying strict mode today!

IE8 Getting EcmaScript 262 rev. 5 Compliant Native JSON

Earlier this week, Microsoft posted an article with details on udpates to the native JSON implementation in IE8.

Because of the new ECMAScript specification changes, some customers have reported issues. These issues are caused by deviations between the native JSON feature in Internet Explorer 8 and the final specification. An update is now available to address these customer issues and improve JSON interoperability of Internet Explorer 8. This update enables JSON interoperability of Internet Explorer 8 to keep in conformance with the new “ECMAScript, fifth edition” standard specification.

According to the Microsoft Knowledgebase article, these updates are already being pushed via Windows Update to Windows versions with IE8 support.

Getting standards compliant native JSON support in what is soon to become the most popular browser on the planet, will certainly make lives easier for everyone working with JSON in browsers, and will help keep our JavaScript frameworks lean and fast.

Why should I care?

This update might seem like small potatoes at first sight, but this update hints at something much, much bigger.

To my knowledge, this is the first time that Microsoft have fixed non-compliant implementations in published versions of Internet Explorer. It also means that IE8 wont’ be bug-for-bug compatible between builds, which is perfectly acceptable to me. Developers deal with browser inconsistencies every day, but it’s not every day we get news as significant as this.

This shift in strategy, could actually mean that Microsoft plan on continually updating the current Internet Explorer with smaller enhancements, and leave the previous strategy of only improving standards support with a new generation of IE.

Who knows, they might even be planning a v8.1 of Internet Explorer, with even more significant upgrades to standards support?

Juicer 1.0.0 Released

Earlier today Christian Johansen pushed the button and published Juicer v1.0.0 as a gem.

For those unfamiliar with Juicer, it’s an open source Ruby based tool that allows you to merge and minify your JavaScript and CSS files. Internally, Juicer uses JSLint to keep your JavaScript in good shape and supports both YUI Compressor and Google Closure Compiler to make your CSS and JavaScript files as small as possible.

I have written about speeding up your Webby site with Juicer previously, and with the new version there are even more features to like. These are the new features that exites me the most

  • Support for Google Closure Compiler
  • Support for embedding images into stylesheets using data-uri’s (my contribution)
  • Ruby 1.9 support

Christian has done a lot of work tidying up the codebase, making it very flexible and easy to work with. So if Juicer also exites you, I suggest you get involved in making it even better.

Speeding Up Your Webby Site With Juicer

On this blog I use several stylesheets to keep things (somewhat) organised. This allows me to upgrade my coderay.css file or my Tripoli CSS stylesheets without having to reorganise everything.

But, just because I like to organise my code into managable chunks, doesn’t mean that I have to degrade the performance of the site for the visitors.

To help me improve performance by both reducing total transfer size and amount of requests for stylesheets, I’ve added Juicer to the mix.

Juicer requires a few gems and Java (as it uses YUI Compressor internally to minify stylesheets), but if you’re still reading, this should not be a problem for you.

The stylesheets

Firstly, I’ve created two driver stylesheets, that is responsible for importing the other stylesheets in the correct order.

First, the stylesheet for modern browsers.

1 /* master.modern.css - holds stylesheets for modern browsers */
2 @import url(/stylesheets/tripoli/tripoli.simple.css);
3 @import url(/stylesheets/coderay.css);
4 @import url(/stylesheets/base.css);

Second, the stylesheet older versions of Internet Explorer

1 /* master.ie7.css - loads stylesheets for IE7 and lower */
2 @import url(/stylesheets/tripoli/tripoli.simple.css);
3 @import url(/stylesheets/tripoli/tripoli.simple.ie.css);
4 @import url(/stylesheets/coderay.css);
5 @import url(/stylesheets/base.css);
6 @import url(/stylesheets/ie7.css);

Steve Souders has explained that you should not use @import in your stylesheets, as it will block parallel downloads. But, don’t worry, Juicer will combine all of these stylesheets into one and all will be well in the world.

The rake file

To control the behaviour of Juicer, I’ve created a little rake task.

 1 # juicer.rake
 2 namespace :juicer do
 3   namespace :merge do
 4     desc 'Merges stylesheets'
 5     task :stylesheets do
 6       sh 'juicer merge content/stylesheets/master.modern.css -o content/stylesheets/master.modern.min.css --document-root content --force'
 7       sh 'juicer merge content/stylesheets/master.ie7.css -o content/stylesheets/master.ie7.min.css --document-root content --force'
 8     end
 9   end
10 end

You should update the rake task with the correct paths and filenames for your site.

Updating the Sitefile

In order for Webby to generate these new files, you will need to update your Sitefile, and instruct Webby to run the merge task with every build. To do this you simply add a dependency for the built-in build tasks.

 1 # add these lines to your Sitefile
 2 desc "Build the website"
 3 task :build => [:configure_basepath, 'juicer:merge:stylesheets' ]
 4 
 5 desc "Rebuild the website"
 6 task :rebuild => [:configure_basepath, 'juicer:merge:stylesheets' ]
 7 
 8 desc "Continuously build the website"
 9 task :autobuild => [:configure_basepath, 'juicer:merge:stylesheets' ]

Using the new merged stylesheets couldn’t be easier, you just reference them by their new names master.modern.min.css and master.ie7.min.css.

Results

For this blog, the result is ~2kb and 2 or 4 connection requests saved, if your site has bigger and/or more stylesheets, you’re going to see even better results.

As an added bonus, Juicer also adds cache buster suffixes to all image references in the stylesheets (this is configurable), so now I can safely turn on expiration headers for these images and my (repeat) visitors can enjoy an even faster site.

Perfect Pitch

Perfect Pitch is an article by Jeremy Keith that discusses some recent issues and misuses of DMCA unfairly to destroy competitors search engine rankings.

It all started out as an innocent comment about attaining Perfect Pitch on The Session.

This is really just another one of those examples of American legislation that got implemented without any real thought of the consequences or of how it could be misused, not entirely unlike the Intellectual Property and Patents legislation in the U.S. GO Lobbyists!!!

Help Jeremy out, and create your own Perfect Pitch post and even better, link to the other two.