检测HTML5< canvas>的最佳方式不支持 [英] Best way to detect that HTML5 <canvas> is not supported

查看:599
本文介绍了检测HTML5< canvas>的最佳方式不支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理浏览器不支持HTML5 < canvas> 标记的情况的标准方法是嵌入一些后备内容,如:

 < canvas>您的浏览器不支持canvas。< / canvas> 

但页面的其余部分保持不变,这可能不恰当或具有误导性。我想要一些方法来检测画布不支持,以便我可以相应地呈现我的页面的其余部分。


解决方案

这是Modernizr中使用的技术,基本上是每个其他的画布工作的库:

  function isCanvasSupported(){
var elem = document.createElement('canvas');
return !!(elem.getContext& elem.getContext('2d'));
}

由于您的问题是不是支持,我建议使用它像这样:

  if(!isCanvasSupported()){... 


The standard way to deal with situations where the browser does not support the HTML5 <canvas> tag is to embed some fallback content like:

<canvas>Your browser doesn't support "canvas".</canvas>

But the rest of the page remains the same, which may be inappropriate or misleading. I'd like some way of detecting canvas non-support so that I can present the rest of my page accordingly. What would you recommend?

解决方案

This is the technique used in Modernizr and basically every other library that does canvas work:

function isCanvasSupported(){
  var elem = document.createElement('canvas');
  return !!(elem.getContext && elem.getContext('2d'));
}

Since your question was for detection when it's not supported, I recommend using it like so:

if (!isCanvasSupported()){ ...

这篇关于检测HTML5&lt; canvas&gt;的最佳方式不支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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