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.
- Check navigator
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.
- Check window 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.
- Check for CocoonJS
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.