Jez D Jez D - 6 months ago 17
Javascript Question

Javascript confirm dialog on Windows 8.1 Store App

I am currently creating a Windows 8.1 Store App, using html5, bing maps, javascript, phonegap & jquery.

I have a button in my app which, when clicked, calls the following function

function resetFields() {
var svar;
var tryk = confirm("Are you sure!");
if (tryk === true) {
svar = "You pressed OK!";
} else {
svar = "You pressed Cancel!";
}
alert(svar);
}


The function is called fine and I see the confirm dialog as one would expect. However, no matter which button is pressed, the result is the same - the dialog responds as if the 'cancel' button has been pressed.

I have tried
if (tryk === true)
and
if (tryk == true)
but the result is the same.

What am I missing?

Even
alert(confirm("Are you sure!"))
is returning 'undefined' so I think my answer lies deeper than the basic code.

Answer

I was just going through some PhoneGap documentation and because it is built around html5 and javascript with jquery support, it tends to over-ride certain elements that we would normally see in the browser to take advantage of the native device elements like a confirm dialog.

From the Notification confirm api docs:

Function notification.confirm displays a native dialog box that is more customizable than the browser's confirm function.

Now, I think the variable tryk is undefined probably because the request returns immediately as soon as any button is clicked on the confirm dialog and that's why you can't properly exploit the handler functions.

In the api docs, they have given a quick example which looks like:

 // process the confirmation dialog result
function onConfirm(button) {
    alert('You selected button ' + button);
}

// Show a custom confirmation dialog
//
function showConfirm() {
    navigator.notification.confirm(
    'You are the winner!',  // message
    onConfirm,              // callback to invoke with index of button pressed
    'Game Over',            // title
    'Restart,Exit'          // buttonLabels
    );
}

And the showConfirm() method can be triggered from any html element like so:

 <a href="#" onclick="showConfirm(); return false;">Show Confirm</a>

So I think you should stick to the implementation they have officially provided. I just stumbled upon another SO Question where it says that PhoneGap confirm actually does an asynchronous callback.