Javascript scopes in coffeescript for Google Analytics Code -
the codes google analytics use global _gaq
object analytics commands. advise check if such object exists, this:
var _gaq = _gaq || []; // command _gaq.push(['_trackpageview']);
in coffeescript, this:
_gaq = _gaq or []
which compiles this:
(function() { var _gaq; _gaq = _gaq || []; }).call(this);
how can write coffeescript code lead behaviour of above javascript?
to make _gaq
variable available in global scope write in coffeescript:
_gaq = window._gaq ?= []
the javascript output:
var _gaq, _ref; _gaq = (_ref = window._gaq) != null ? _ref : window._gaq = [];
this way can latter call _gaq.push(['_trackpageview']);
there another question in stackoverflow talks global variables in coffeescript might want check.
Comments
Post a Comment