Avoid a bit of code duplication in various .js files
Change-Id: I14e28a85872e0248ab85aeb049ea89689653501d