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.
Solution 1:
if ('options'indocument.createElement('datalist')) {
// supported!
}
Solution 2:
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 = functionisElementSupported(tag) {
// Return undefined if `HTMLUnknownElement` interface// doesn't existif (!win.HTMLUnknownElement) {
returnundefined;
}
// Create a test element for the tagvar 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 supportedreturn toString.call(element) !== '[object HTMLUnknownElement]';
};
})(this);
Solution 3:
Check if the browser support the HTMLDataListElement interface:
if(typeof HTMLDataListElement === 'function') {
// your code here
} else {
// your code here if this feature is not supported
}
Post a Comment for "Check If Html Element Is Supported"