All HTML Tags supported by a Browser

后端 未结 4 1235
心在旅途
心在旅途 2020-12-11 02:23

Using JavaScript, is it possible to obtain a list of the tags that a browser supports?

4条回答
  •  心在旅途
    2020-12-11 03:20

    If you're willing to start with a known list of candidate tags, you could try something like this:

    document.createElement("asdf") instanceof HTMLUnknownElement
    true
    document.createElement("canvas") instanceof HTMLUnknownElement
    false
    

    If you need to support IE8, you could use this approach:

    function browserSupports(elementTagName) {
        var el = document.createElement(elementTagName);
        return !((el instanceOf HTMLUnknownElement) || (el instanceof HTMLGenericElement));
    }
    

    Here's another approach that doesn't rely on specific named constructors.

    function browserSupports(elementTagName) {
        var unknownel = document.createElement("zzxcv");
        var el = document.createElement(elementTagName);
        return unknownel.constructor !== el.constructor;
    }
    

    It still doesn't seem to work in IE8 though.

提交回复
热议问题