How can I tell if a browser supports <input type='date'>

后端 未结 1 2032
臣服心动
臣服心动 2020-11-28 05:42

Possible Duplicate:
HTML5 Type Detection and Plugin Initialization


<         


        
相关标签:
1条回答
  • 2020-11-28 06:14

    The method bellow checks if some input type is supported by most of the browsers:

    function checkInput(type) {
        var input = document.createElement("input");
        input.setAttribute("type", type);
        return input.type == type;
    }
    

    But, as simonox mentioned in the comments, some browsers (as Android stock browsers) pretend that they support some type (as date), but they do not offer an UI for date inputs. So simonox improved the implementation using the trick of setting an illegal value into the date field. If the browser sanitises this input, it could also offer a datepicker!!!

    function checkDateInput() {
        var input = document.createElement('input');
        input.setAttribute('type','date');
    
        var notADateValue = 'not-a-date';
        input.setAttribute('value', notADateValue); 
    
        return (input.value !== notADateValue);
    }
    
    0 讨论(0)
提交回复
热议问题