如何在浏览器中检测对VML或SVG的支持 [英] How do you detect support for VML or SVG in a browser
问题描述
我正在编写一些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元素(< v:shape>
)。看起来它是对邻接属性的测试这可以确定创建的元素是否真正被解释为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屋!