How to detect unofficial tags in html?

  • A+
Category:Languages

Given an HTML node, how would you tell if it bears official HTML tag or not?

<h9 id="someNodeId">hello<h9> let node = document.getElementById("someNodeId"); 

In above code snippet I want h9 is not official html tag. How do I find it out programmatically using JS?

Edit: Preferably in O(1)

 


Someone already wrote a good function for this, see usage guide on GitHub.

Example:

isElementSupported("h1"); // true isElementSupported("h9"); // false 
/*  * 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);
Tag: <input id="toCheck" type="text" value="h9"><br><br> Is supported? <input id="result" type="text" readonly><br><br> <input type="submit" value="Check Tag" onclick="document.getElementById('result').value= (isElementSupported(document.getElementById('toCheck').value))">

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: