Skip to content

Instantly share code, notes, and snippets.

@DataZombies
Last active October 12, 2015 10:38
Show Gist options
  • Save DataZombies/4014424 to your computer and use it in GitHub Desktop.
Save DataZombies/4014424 to your computer and use it in GitHub Desktop.
PhoneGap Replacements for window.alert & window.confirm
// This code gives you a browser alert and confirm or a Cordova alert and confirm dialog boxes depending on the platform
// app-wide constants & modified functions
var APP_NAME = 'A Truly GREAT app',
DESKTOP = true, // a constant controlling whether browser alert and confirm or Cordova alert and confirm are used.
ConfirmReturnValue = -1,
Alert = function (message) {
alert (message);
},
Confirm = function(message) {
ConfirmReturnValue = -1;
ConfirmReturnValue = confirm(message);
},
replaceAlertAndConfirm = function () {
Alert = function (message, title, buttonName) {
title = title || APP_NAME;
navigator.notification.alert(message, null, title, buttonName);
};
Confirm = function(message, title, buttonLabels) {
function ConfirmCallback (buttonIndex) {
switch (buttonIndex) {
case 1: ConfirmReturnValue = true; break;
case 2: ConfirmReturnValue = false; break;
default: ConfirmReturnValue = buttonIndex; break;
}
};
ConfirmReturnValue = -1;
title = title || APP_NAME;
navigator.notification.confirm(message, ConfirmCallback, title, buttonLabels);
};
ConfirmWithCallback = function(message, confirmCallback, buttonLabels) {
navigator.notification.confirm(message, confirmCallback, APP_NAME, buttonLabels);
};
};
Alert('This is an alert', 'Test Alert');
Confirm('This is a confirm. Click Yes to continue.', 'Test Confirm', 'Yes,No'); // you need a timeout function to capture the Yes
waitForConfirmReturnValue ();
var waitForConfirmReturnValue = function () {
var temp, timer;
timer = setTimeout(function () {
if (ConfirmReturnValue !== -1) {
temp = ConfirmReturnValue;
ConfirmReturnValue = -1;
clearTimeout(timer);
DoSomethingWithTheReturnValue (temp);
} else {
waitForConfirmReturnValue();
}
}, 500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment