如何检测浏览器中对 VML 或 SVG 的支持
我正在编写一些 javascript,需要在 SVG 或 VML 之间进行选择(或两者,或其他,这是一个奇怪的世界)。 虽然我知道目前只有 IE 支持 VML,但我更愿意检测功能而不是平台。
SVG 似乎有一些您可以使用的属性:例如 window.SVGAngle。
这是检查 SVG 支持的最佳方法吗?
VML 有等效的吗?
不幸的是 - 在 Firefox 中我可以很高兴地在 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 appears to have a few properties which you can go for: window.SVGAngle for example.
Is this the best way to check for SVG support?
Is there any equivalent for 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
另一方面...
当您想在提供页面之前了解情况时:
抓取此页面:
http://caniuse.com/#cats=SVG&statuses=rec&nodetails =1
对于传入浏览器/用户代理。
免责声明:
我还没有实现这个。
我希望 caniuse.com 能够发布一个 api 来使用。
马克特
On the other hand...
When you want to know before you serve the page:
Scrape this page:
http://caniuse.com/#cats=SVG&statuses=rec&nodetails=1
For the incoming browser/user agent.
Disclaimer:
I've not yet implemented this.
As I'm hoping caniuse.com will publish an api to work with.
MarkT
我建议对 Crescentfresh 的答案进行一项调整 - 使用
而不是
检测 SVG。 WebKit 目前对报告功能非常挑剔,尽管具有相对可靠的 SVG 支持,但仍会为
feature#Shape
返回 false。 https 的评论中建议使用feature#BasicStructure
替代方案: //bugs.webkit.org/show_bug.cgi?id=17400 并给出了我在 Firefox/Opera/Safari/Chrome (true) 和 IE (false) 上期望的答案。请注意,
implementation.hasFeature
方法将忽略通过插件的支持,因此,如果您想检查例如 IE 的 Adobe SVG Viewer 插件,则需要单独执行此操作。 我想 RENESIS 插件也是如此,但尚未检查。I'd suggest one tweak to crescentfresh's answer - use
rather than
to detect SVG. WebKit is currently very picky about reporting features, and returns false for
feature#Shape
despite having relatively solid SVG support. Thefeature#BasicStructure
alternative is suggested in the comments to https://bugs.webkit.org/show_bug.cgi?id=17400 and gives me the answers I expected on Firefox/Opera/Safari/Chrome (true) and IE (false).Note that the
implementation.hasFeature
approach will ignore support via plugins, so if you want to check for e.g. the Adobe SVG Viewer plugin for IE you'll need to do that separately. I'd imagine the same is true for the RENESIS plugin, but haven't checked.SVG 检查在 Chrome 中对我不起作用,所以我查看了 Modernizer 库在其检查中的作用 (https://github.com/Modernizr/Modernizr/blob/master/modernizr.js)。
根据他们的代码,这对我有用:
The SVG check didn't work for me in Chrome, so I looked at what the Modernizer library does in their check (https://github.com/Modernizr/Modernizr/blob/master/modernizr.js).
Based on their code, this is what worked for me:
对于 VML 检测,Google 地图的操作如下(搜索“
function Xd
”):我明白您对 FF 的意思:它允许创建任意元素,包括 vml 元素(
) 。 看起来这是对 邻接属性 的测试可以确定创建的元素是否真正被解释为 vml 对象。对于 SVG 检测,这很有效:
For VML detection, here's what google maps does (search for "
function Xd
"):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.For SVG detection, this works nicely:
您可能想跳过此步骤并使用 JS 库,它允许您跨浏览器进行矢量绘图(如果您有意这样做)。 然后,库将处理此问题,如果支持,则输出到 SVG;如果不支持,则回退到 canvas、VML、flash、silverlight 等,具体取决于可用的内容。
执行此操作的库示例如下(排名不分先后):
You might like to skip this and use a JS library which will allow you to do vector drawing cross-browser, if that's the intention. The library will then handle this, outputting to SVG if supported or fallback to canvas, VML, flash, silverlight, etc if not, depending on what's available.
Examples of libraries that will do this are, in no particular order:
如果您假设非 SVG 浏览器是 IE5.5 或更高版本并且可以支持 VML,则可以使用。
在 IE6、Firefox 8、Chrome 14.0 上测试。
Raphael 非常酷,但它不支持组的概念,这可能会受到限制,具体取决于您正在做什么。
不过,德米特里可能会因为我这么说而责骂我。
Works, if you assume that non-SVG browsers are IE5.5 or better and can support VML.
Tested on IE6, Firefox 8, Chrome 14.0.
Raphael is very cool, but it does not support the concept of groups, which can be limiting depending on what you are doing.
Dmitry will probably flame me for saying so, though.
您可能需要查看 http://www.modernizr.com/docs/#features-misc 因为它包含对 SVG 功能实际检测的支持,而不是容易损坏的用户代理嗅探。
You may want to check out http://www.modernizr.com/docs/#features-misc as it contains support for actual detection of SVG capability as opposed to user-agent sniffing which can be easily corrupted.
SVG 检查在 Chrome 中不起作用,因为它指定版本 1.0。 这应该效果更好:
The SVG-check did not work in Chrome because it specifies version 1.0. This should work better: