How to determine if window.confirm() is supported?

后端 未结 5 2018
小鲜肉
小鲜肉 2021-01-23 06:09

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 ca

相关标签:
5条回答
  • 2021-01-23 06:17

    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?");
        });
    }
    
    0 讨论(0)
  • 2021-01-23 06:25
    if(typeof confirm=='function')// window.confirm is defined and a function
    
    0 讨论(0)
  • 2021-01-23 06:27

    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.

    0 讨论(0)
  • 2021-01-23 06:35

    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').

    0 讨论(0)
  • 2021-01-23 06:41

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

    0 讨论(0)
提交回复
热议问题