Fixing Jasmine “encountered a declaration exception”

If you ever come upon a Jasmine unit test that ends with “encountered a declaration exception”, it’s likely because you have committed a simple, subtle mistake. Every few weeks, I would get that error, and forgetting how I fixed it the previous time, spend another 30 minutes to fix it. Here’s a screenshot of the […]

How to create static class constants in ES6

ES6 brings classes to JavaScript, but some things are not immediately obvious or entirely supported. For instance, there is no obvious way to create static class constants. Here is the simplest way I have found to create constants: class CurrentLocation { static get GEOLOCATION_ERROR() { return ‘GEOLOCATION_ERROR’; } static get GEOLOCATION_REFUSED() { return ‘GEOLOCATION_REFUSED’; } […]

How to fix “Cannot read property ‘failedExpectations’ of undefined”

While running Karma unit tests, you might run into the following error: Uncaught TypeError: Cannot read property ‘failedExpectations’ of undefined. The cryptic error message might be a pain in the butt to debug, so let me save you some time. In your tests, look for reassignment of the result variable. You are probably overwriting a […]

How to throttle scroll events in AngularJS

If you watch the scroll event, you will probably find yourself handling far more events than you need. The scroll event fires really quickly, and that can be a problem on mobile devices. The following Angular directive will call a specified scroll event every 250 milliseconds. angular.module(‘hereApp.directive’).directive(‘onScroll’, function($timeout) { ‘use strict’; return { scope: { […]

Disable “pull to refresh” in Chrome for Android

In mobile Chrome, pulling the top of the page will reload it. Although it’s a convenient UI pattern, it might get in the way if you are trying to implement gestures in your app. After careful observation, I have come up with a simple solution. AngularJS I have successfully disabled “pull to reload” with this […]

How to replace accented characters in a Javascript string

If you are dealing with international user, you will sometimes need to replace unicode characters (éåü) with their ascii counterparts (eau). The easiest way is to use a small library called stringops. It extends the String prototype to give your strings the .noAccents method. For instance, you can do “éåü”.noAccents() and you will get “eau”. […]

input.select() does not work on iOS

The recommended method to give a text field focus and select its contents is the following: document.getElementById(‘myInput’).select(); However, this does not work in iOS. The correct way to do it is to use setSelectionRange on an already focused input. var input = document.getElementById(‘myInput’); input.focus(); input.setSelectionRange(0,99999); setSelectionRange will not work on iOS if the element doesn’t […]

Fixing “/usr/bin/env: node: No such file or directory”

After a fresh install of npm and bower, you may run into the following error while running bower install: /usr/bin/env: node: No such file or directory The fix on Ubuntu is very simple: sudo ln -s /usr/bin/nodejs /usr/bin/node

.find() vs. .children(): which one should you use?

jQuery offers two functions to find children in an element: .find() and .children(). .find() will look through all children of an element while .children() will only look at immediate children. <ul> <li> <p></p> </li> <li> <p></p> </li> <li> <p></p> </li> </ul> In the snippet above, $(‘ul’).children(‘p’) wouldn’t return anything, while $(‘ul’).find(‘p’) would return all three […]

Javascript, jQuery and parent windows

When you open a window with window.open, you can access the popup’s parent window using window.opener from the child window. //In the parent window window.open(‘http://example.com/url’); //Opens a new window/tab with this URL //In the opened window window.opener.document; You can perform the same requests on window.opener.document as you would on the document object: window.opener.document.getElementById(‘#tableInParent’); //Gets #tableInParent […]