How do you detect support for VML or SVG in a browser

前端 未结 8 1410
春和景丽
春和景丽 2020-11-29 18:12

I\'m writing a bit of javascript and need to choose between SVG or VML (or both, or something else, it\'s a weird world). Whilst I know that for now that only IE supports VM

8条回答
  •  伪装坚强ぢ
    2020-11-29 18:30

    For VML detection, here's what google maps does (search for "function Xd"):

    function supportsVml() {
        if (typeof supportsVml.supported == "undefined") {
            var a = document.body.appendChild(document.createElement('div'));
            a.innerHTML = '';
            var b = a.firstChild;
            b.style.behavior = "url(#default#VML)";
            supportsVml.supported = b ? typeof b.adj == "object": true;
            a.parentNode.removeChild(a);
        }
        return supportsVml.supported
    }
    

    I see what you mean about FF: it allows arbitrary elements to be created, including vml elements (). It looks like it's the test for the adjacency attribute that can determine if the created element is truly interpreted as a vml object.

    For SVG detection, this works nicely:

    function supportsSvg() {
        return document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Shape", "1.0")
    }
    

提交回复
热议问题