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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
var isCocoonJS = (typeof window.CocoonJS !== "undefined"); - Check for CocoonJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
var isCocoonJS = (typeof CocoonJS !== "undefined"); 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.