Last active
August 29, 2015 14:00
-
-
Save elidupuis/11268907 to your computer and use it in GitHub Desktop.
Combining Cordova deviceready and domready in a Component(1) app
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
{ | |
"locals": [ | |
"foobar" | |
], | |
"dependencies": { | |
"jb55/domready": "*" | |
}, | |
"scripts": [ | |
"index.js" | |
] | |
} |
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 domready = require('domready'); | |
var foobar = require('foobar'); | |
var isPhone = false; | |
// see http://stackoverflow.com/questions/12576062/jquery-document-ready-vs-phonegap-deviceready#answer-14109006 | |
domready(function() { | |
// are we running in native app or in a browser? | |
if('file:' === document.location.protocol) { | |
isPhone = true; | |
} | |
if(isPhone) { | |
document.addEventListener("deviceready", onDeviceReady, false); | |
} else { | |
onDeviceReady(); | |
} | |
}); | |
function onDeviceReady () { | |
console.log('deviceready! get it on.') | |
foobar.init(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there any difference between
document.location.href
anddocument.URL
?Also, it's probably simpler to set
isphone
to true if'file:' === document.location.protocol
; at least for Cordova apps...And I'd prefer if
isphone
wasn't on the window object.