如何在浏览器中检测对 VML 或 SVG 的支持 [英] How do you detect support for VML or SVG in a browser

查看:25
本文介绍了如何在浏览器中检测对 VML 或 SVG 的支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些 javascript,需要在 SVG 或 VML(或两者兼而有之,或其他什么,这是一个奇怪的世界)之间做出选择.虽然我知道目前只有 IE 支持 VML,但我更愿意检测功能而不是平台.

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 VML, I'd much rather detect functionality than platform.

SVG 似乎有一些您可以使用的属性:例如 window.SVGAngle.

SVG appears to have a few properties which you can go for: window.SVGAngle for example.

这是检查 SVG 支持的最佳方式吗?

Is this the best way to check for SVG support?

VML 有什么等价物吗?

Is there any equivalent for VML?

不幸的是 - 在 Firefox 中,我可以很高兴地在 VML 中完成所有渲染而不会出错 - 只是屏幕上没有任何反应.从脚本中很难检测到这种情况.

Unfortuntaly - in firefox I can quite happily do all the rendering in VML without error - just nothing happens on screen. It's quite hard to detect that situation from script.

推荐答案

对于 VML 检测,这里是 谷歌地图确实(搜索function Xd"):

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 = '<v:shape id="vml_flag1" adj="1" />';
        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
}

我明白你对 FF 的意思:它允许创建任意元素,包括 vml 元素().看起来这是对邻接属性的测试可以确定创建的元素是否真的被解释为 vml 对象.

I see what you mean about FF: it allows arbitrary elements to be created, including vml elements (<v:shape>). 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.

对于 SVG 检测,这很有效:

For SVG detection, this works nicely:

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

这篇关于如何在浏览器中检测对 VML 或 SVG 的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆