HomeRamblings  ⁄  ProgrammingJavaScript


Adding Google Analytics script to Sprockets

Today, I wanted to add Google Analytics for tracking my website's usage history. Rails 4.0 has an excellent assets pipeline that generally works well out of the box with little fuss, but when you try to do something outside adding another require to the "assets/javascripts/application.js.coffee" file, it gets a little more complicated to implement. Here, I show you exactly how to add Google Analytics as a coffeescripted script that is only loaded in the production environment. ## Preamble One of the things I _don't_ do is use sprockets' "require_tree ." directive because this tends to pick up everything I have in the assets folders for every rendering and that's just not necessary. Even though uglifier and sprockets does a good job of compressing and minifying the javascript code, I rarely need more than a handful of those assets on the… read more

Cross-browser AJAX updates to table elements

It seems that one of the toughest Javascript challenge is to get your AJAX code consistently behave between browsers when you're dealing with IE's handling of table elements vs. Gecko and other engines. It took quite a bit of finagling to figure out exactly where the issues and trappings are. Most web developers know about IE's, ahem, rather unque way of handling tables and the likes, but what's a web application to do if (s)he is gonna bring some sanity to the picture? Well, Mootools 1.2 goes a long ways towards easing your misery in terms of interacting with the DOM and doing some really cool Javascript things in a consistent manner, but you still have to figure out the specifics that you're encountering for your application's needs. My needs were simple. I wanted to use AJAX to pop up… read more

Conway's Game of Life in CoffeeScript

I was recently invited by Jonathan Wallace to a local Code Retreat where the developers were going to tackle Tim Conway's famous Game of Life  Unfortunately, I was unable to attend, but the whole idea sparked my own self-exploration into both CoffeeScript and Middleman, which I had little practice and practical experience with.  You can find the resulting code in its entirety at https://github.com/mwlang/life_with_coffeescript I learned some interesting things about CoffeeScript along the way as well as DOM manipulation and JavaScript performance issues that are worth sharing. In case you have not seen the game of life before, below is a screen shot of the game in action with a world that is 25 x 25.   The above shows a simple form that allows the size of the world grid to be changed… read more