Lessons learned from detecting CocoonJS

After writing yesterday’s post, I realized I should probably write up my notes from detecting CocoonJS as well. And like my posts on Apache Cordova and Node-Webkit, this too will cover the multiple ways to detect the CocoonJS environment depending on what else might be loaded in the same context.

  1. Check navigator

    var isCocoonJS = (typeof navigator['isCocoonJS'] !== "undefined");
    view raw nav.js hosted with ❤ by GitHub

    Checking for the isCocoonJS property of the global navigator object is both the easiest and most reliable way to detect CocoonJS. Before any other code is loaded, this will either exist or not. And if it does, the environment is probably CocoonJS.

  2. Check window

    var isCocoonJS = (typeof window.CocoonJS !== "undefined");
    view raw window.js hosted with ❤ by GitHub

    Checking for the CocoonJS property of the window object is how Ludei (makers of CocoonJS) check in their own code. If it exists, there is a high probability that the environment is CocoonJS.

  3. Check for CocoonJS

    var isCocoonJS = (typeof CocoonJS !== "undefined");
    view raw cjs.js hosted with ❤ by GitHub

    This method can be as effective as the above. However, it does require that “CocoonJS.js” be loaded before this detection takes place because of the reassignment of the window.CocoonJS global to the CocoonJS object during that code’s execution.