2

I have a mobile website that is used by various devices including some onboard computers running a locked down version of Windows Embedded 7 with IE 7. For some reason that I cannot explain, window.confirm() is broken, yet all other javascript seems to work.

I even added the following check before wiring up the confirm handler, but clicking the link simply does nothing.

        if (window.confirm)
        {
            $(".logoff").click(function () 
            {
                return confirm("Are you sure you want to log off?");
            });
        }

If I remove the click handler, the link functions as normal. Is there a better way to test for confirm() support?

jrummell
  • 42,637
  • 17
  • 112
  • 171

5 Answers5

1

How about:

if ('confirm' in window) {
    $(".logoff").click(function () {
        return window.confirm("Are you sure you want to log off?");
    });
}

Another options would be window.hasOwnProperty('confirm').

David Hellsing
  • 106,495
  • 44
  • 176
  • 212
1

You could use typeof to check if the window.confirm is a function

VirtualTroll
  • 3,077
  • 1
  • 30
  • 47
1

You could do

if ('confirm' in window && typeof window.confirm === 'function' ) {
    $(".logoff").click(function () {
        return window.confirm("Are you sure you want to log off?");
    });
}
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
1
if(typeof confirm=='function')// window.confirm is defined and a function
kennebec
  • 102,654
  • 32
  • 106
  • 127
0

While the following suggested methods worked in every desktop browser I tested, they all evaluated to true on the device and still didn't display a dialog or navigate to the link in the anchor's href.

if (typeof confirm == 'object')
if ('confirm' in window && typeof window.confirm === 'object')
if (window.confirm)

(In IE 7, typeof somefunction is 'object' instead of 'function')

My solution was to use a jQuery UI Dialog instead, based on @ThiefMaster's suggestion.

jrummell
  • 42,637
  • 17
  • 112
  • 171