Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if html element is supported

How to check if html element is supported, datalist for example? MDC says it is supoorted only in Opera and FireFox.

like image 941
Bakudan Avatar asked Aug 13 '11 02:08

Bakudan


2 Answers

if ('options' in document.createElement('datalist')) {
    // supported!
}

http://diveintohtml5.info/everything.html#datalist

like image 155
deceze Avatar answered Nov 05 '22 12:11

deceze


If someone needs to check for support of other HTML5 elements this could also be used.

https://github.com/ryanmorr/is-element-supported

From http://ryanmorr.com/determine-html5-element-support-in-javascript/

/*
 * isElementSupported
 * Feature test HTML element support 
 * @param {String} tag
 * @return {Boolean|Undefined}
 */

(function(win){
    'use strict';       

    var toString = {}.toString;

    win.isElementSupported = function isElementSupported(tag) {
        // Return undefined if `HTMLUnknownElement` interface
        // doesn't exist
        if (!win.HTMLUnknownElement) {
            return undefined;
        }
        // Create a test element for the tag
        var element = document.createElement(tag);
        // Check for support of custom elements registered via
        // `document.registerElement`
        if (tag.indexOf('-') > -1) {
            // Registered elements have their own constructor, while unregistered
            // ones use the `HTMLElement` or `HTMLUnknownElement` (if invalid name)
            // constructor (http://stackoverflow.com/a/28210364/1070244)
            return (
                element.constructor !== window.HTMLUnknownElement &&
                element.constructor !== window.HTMLElement
            );
        }
        // Obtain the element's internal [[Class]] property, if it doesn't 
        // match the `HTMLUnknownElement` interface than it must be supported
        return toString.call(element) !== '[object HTMLUnknownElement]';
    };

})(this);
like image 1
lowtechsun Avatar answered Nov 05 '22 13:11

lowtechsun