Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Mirror of Apache Cordova js
JavaScript Python HTML CSS
Pull request Compare This branch is 5 commits ahead, 267 commits behind master.
Failed to load latest commit information.
tasks renaming reporter to test-reporter
test removed old runner logic into tasks
.gitignore [CB-3193] [BlackBerry10] Remove all plugins from cordova-js
.jshintrc [all] CB-3305 Use packager to set device.cordova
.reviewboardrc Add a .reviewboardrc file to make using post-review easier.
Gruntfile.js temporarily removed btests, not working for me
LICENSE added test cases for require() and friends
NOTICE added test cases for require() and friends revised readme to reflect changes to structure and gruntfile
VERSION Set VERSION to 3.4.0 (via coho)
package.json CB-5307 Remove references to Callback and Incubator

A unified JavaScript layer for Apache Cordova projects.

Project Structure

 |  |-cordova.js ........ common Cordova stuff
 |  |-common/ ........... base modules shared across platfoms
 |  |  |-builder.js ..... injects in our classes onto window and navigator
 |  |  |-channel.js ..... pub/sub impl for custom framework events 
 |  |  |-common.js ...... common locations to add Cordova objects to browser globals
 |  |  |-exec.js ........ interace stub for each platform specific version of exec.js
 |  |  |-platform.js .... stub for platform's specific version of platform.js
 |  |  '-utils.js ....... closures, uuids, object, cloning, extending prototypes
 |  |
 |  |-scripts/ .......... non-module JS that gets concated to cordova.<platform>.js
 |  |  |-bootstrap.js ... bootstrap the Cordova platform, inject APIs and fire events
 |  |  '-require.js ..... module definition and require() impl
 |  |
 |  '-<platform>/ ....... contains the platform-specific base modules
 |-tasks/ ............... custom grunt tasks
 '-tests/ ............... unit tests


Make sure you have node.js installed. It should come pre-installed with npm - but if you install node and can't run npm then head over to the website and install it yourself. Make sure you have all of the node dependencies installed by running the following command from the repository root:

npm install

All of the build tasks can be run via the grunt node module. Install it globally first by running:

sudo npm install -g grunt-cli

Then from the repository root run:


Known Issues

  • On Windows, when you run npm install, you may get errors regarding contextify. This is necessary for running the tests. Make sure you are running node v0.6.15 at the least (and npm v1.1.16 which should come bundled with node 0.6.15). Also, install Python 2.7.x and Visual C++ 2010 Express. When that is done, run npm install again and it should build contextify natively on Windows.

How It Works

The build/packager.js tool is a node.js script that concatenates all of the core Cordova plugins in this repository into a cordova.<platform>.js file under the pkg/ folder. It also wraps the plugins with a RequireJS-compatible module syntax that works in both browser and node environments. We end up with a cordova.js file that wraps each Cordova plugin into its own module.

Cordova defines a channel module under lib/common/channel.js, which is a publish/subscribe implementation that the project uses for event management.

The Cordova native-to-webview bridge is initialized in lib/scripts/bootstrap.js. This file attaches the boot function to the channel.onNativeReady event - fired by native with a call to:


The boot method does all the work. First, it grabs the common platform definition (under lib/common/common.js) and injects all of the objects defined there onto window and other global namespaces. Next, it grabs all of the platform-specific object definitions (as defined under lib/<platform>/platform.js) and overrides those onto window. Finally, it calls the platform-specific initialize function (located in the platform definition). At this point, Cordova is fully initialized and ready to roll. Last thing we do is wait for the DOMContentLoaded event to fire to make sure the page has loaded properly. Once that is done, Cordova fires the deviceready event where you can safely attach functions that consume the Cordova APIs.


Tests run in node or the browser. To run the tests in node:

grunt test

To run them in the browser:

grunt btest

Final testing should always be done with the Mobile Spec test application.



Build the .js file and drop it in as a replacement for cordova.js.

Adding a New Platform

  1. Add your platform as a directory under the lib folder.
  2. Write a module that encapsulates your platform's exec method and call it exec.js. The exec method is a JavaScript function that enables communication from the platform's JavaScript environment into the platform's native environment. Each platform uses a different mechanism to enable this bridge. We recommend you check out the other platform exec definitions for inspiration. Drop this into the lib/<platform> folder you created in step 1. The exec method has the following method signature: function(success, fail, service, action, args), with the following parameters:
    • success: a success function callback
    • fail: a failure function callback
    • service: a string identifier that the platform can resolve to a native class
    • action: a string identifier that the platform can resolve to a specific method inside the class pointed to by service
    • args: an array of parameters to pass to the native method invoked by the exec call It is required that new platform additions be as consistent as possible with the existing service and action labels.
  3. Define your platform definition object and name it platform.js. Drop this into the lib/<platform> folder. This file should contain a JSON object with the following properties:

    • id: a string representing the platform. This should be the same name the .js file has
    • objects: the property names defined as children of this property are injected into window, and also overrides any existing properties. Each property can have the following child properties:
      • path: a string representing the module ID that will define this object. For example, the file lib/plugin/accelerometer.js can be accessed as "cordova/plugin/accelerometer". More details on how the module IDs are defined are above under the "How It Works" section.
      • children: in a recursive fashion, can have path and children properties of its own that are defined as children of the parent property object
    • merges: similar to the above objects property, this one will not clobber existing objects, instead it will recursively merge this object into the specific target
    • initialize: a function that fires immediately after the objects (see above) are defined in the global scope

    The following is a simple example of a platform definition:

        console.log('firing up Cordova in my Atari, yo.');
  4. You should probably add a packager.bundle('<platform>') call to the Jakefile under the build task.

  5. Make sure your native implementation executes the following JavaScript once all of the native side is initialized and ready: require('cordova/channel')
  6. The deviceready event is important. To make sure that the stock common JavaScript fires this event off, the device and network connection plugins must successfully be instantiated and return information about the connectivity and device information. The success callbacks for these plugins should include calls to require('cordova/channel') (for device information) and require('cordova/channel') (for network information).
  7. Last but certainly not least: add yourself to the contributors list! It's in the package.json file in the root of this repository. You deserve it!
Something went wrong with that request. Please try again.