I am thinking of re-using my phonegap html, css and js code as a web app. I would be going through and removing any mobile only functionalities.
The purpose is to have a
We are developing an iPad app and deployed it as a mobile website too. Whenever PhoneGap specific calls are made, using a common method called isRunningOnPhoneGap() (returns false if the code is running as website) , we decide whether to invoke the PhoneGap feature or to display the web feature. This is how we decide if the app is running as a website or on a mobile device.
var isRunningOnPhoneGap: function () {
if ((document.URL.indexOf('http://') === -1) && (document.URL.indexOf('https://') === -1)) {
if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
Yes it will work.I have tried the vice-versa of your requirement.Including the cordova js file works but with some functionalities not supported.But you will definitely get the basic ones.
With a responsive design your phonegap code should run on almost any device. It's important to know what it is running on (both device and OS) so you can respond accordingly. I build a window.deviceInfo
object up front with the following information:
window.deviceInfo.type
: handheld
, tablet
, desktop
window.deviceInfo.brand
: ios
, android
, microsoft
, webos
, blackberry
window.deviceInfo.mode
: browser
, standalone
, webview
window.deviceInfo.mobile
: true
, false
window.deviceInfo.phonegap
: true
, false
I use a single container <div>
called viewport
to create my responsive container and size it based on the device it's on.
Demo:
This is the initialization code to set everything up:
initializeEnvironment();
initializeDimensions();
initializePhoneGap( function () {
//start app
} );
First I set up window.deviceInfo
.
function initializeEnvironment() {
//window.deviceInfo.type: handheld, tablet, desktop
//window.deviceInfo.brand: ios, android, microsoft, webos, blackberry
//window.deviceInfo.mode: browser, standalone, webview
//window.deviceInfo.mobile: true, false
//window.deviceInfo.phonegap: true, false
var userAgent = window.navigator.userAgent.toLowerCase();
window.deviceInfo = {};
if ( /ipad/.test( userAgent ) || ( /android/.test( userAgent ) && !/mobile/.test( userAgent ) ) ) {
window.deviceInfo.type = 'tablet';
} else if ( /iphone|ipod|webos|blackberry|android/.test( userAgent ) ) {
window.deviceInfo.type = 'handheld';
} else {
window.deviceInfo.type = 'desktop';
};
if ( /iphone|ipod|ipad/.test( userAgent ) ) {
var safari = /safari/.test( userAgent );
window.deviceInfo.brand = 'ios';
if ( window.navigator.standalone ) {
window.deviceInfo.mode = 'standalone';
} else if ( safari ) {
window.deviceInfo.mode = 'browser';
} else if ( !safari ) {
window.deviceInfo.mode = 'webview';
};
} else if ( /android/.test( userAgent ) ) {
window.deviceInfo.brand = 'android';
window.deviceInfo.mode = 'browser';
} else if ( /webos/.test( userAgent ) ) {
window.deviceInfo.brand = 'webos';
window.deviceInfo.mode = 'browser';
} else if ( /blackberry/.test( userAgent ) ) {
window.deviceInfo.brand = 'blackberry';
window.deviceInfo.mode = 'browser';
} else {
window.deviceInfo.brand = 'unknown';
window.deviceInfo.mode = 'browser';
};
window.deviceInfo.mobile = ( window.deviceInfo.type == 'handheld' || window.deviceInfo.type == 'tablet' );
};
Then I resize the viewport
and anything else that needs it. Mobile devices use window.innerWidth
and window.innerHeight
to take up the full screen.
function initializeDimensions() {
var viewport = document.getElementById( 'viewport' );
if ( window.deviceInfo.mobile ) {
viewport.style.width = window.innerWidth + 'px';
viewport.style.height = window.innerHeight + 'px';
} else {
//requirements for your desktop layout may be different than full screen
viewport.style.width = '300px';
viewport.style.height = '300px';
};
//set individual ui element sizes here
};
Finally, I use window.device
(note this is not the same as the deviceInfo
object I create) to verify if phonegap is available and ready. Instead of relying on the finicky deviceready
event, I poll that object when my code is running on a device that should be running phonegap. When the initializePhoneGap()
callback is called, the app is ready to start.
Throughout the app, I wrap phonegap features in if( window.deviceInfo.phonegap ) {}
.
function initializePhoneGap( complete ) {
if ( window.deviceInfo.brand == 'ios' && window.deviceInfo.mode != 'webview' ) {
window.deviceInfo.phonegap = false;
complete();
} else if ( window.deviceInfo.mobile ) {
var timer = window.setInterval( function () {
if ( window.device ) {
window.deviceInfo.phonegap = true;
complete();
};
}, 100 );
window.setTimeout( function () { //failsafe
if ( !window.device ) { //in webview, not in phonegap or phonegap failed
window.clearInterval( timer );
window.deviceInfo.phonegap = false;
complete();
};
}, 5000 ); //fail after 5 seconds
} else {
window.deviceInfo.phonegap = false;
complete();
};
};