How to check, if a HTML5 input is supported?

后端 未结 4 1563
清歌不尽
清歌不尽 2021-01-11 12:47

I want to check in my website, if the visitors browser support a HTML5 input type. How should I do it?

4条回答
  •  心在旅途
    2021-01-11 13:49

    You could do:

    function IsAttributeSupported(tagName, attrName) {
        var val = false;
        // Create element
        var input = document.createElement(tagName);
        // Check if attribute (attrName)
        // attribute exists
        if (attrName in input) {
            val = true;
        }
        // Delete "input" variable to
        // clear up its resources
        delete input;
        // Return detected value
        return val;
    }
    
    if (!IsAttributeSupported("input", "placeholder")) {
        // Do something special here
        alert("placeholder attribute is not supported");
    }
    

    Hope it helps

提交回复
热议问题