检测聚合物

时间:2015-06-05 12:27:56

标签: cross-browser polymer

我在网站上使用Polymer(版本0.5,可能会在某些时候升级到1.0)。显然,许多旧版浏览器与Polyfills不兼容。

有没有办法测试polyfill在特定浏览器中是否成功?因此,在完成polyfill之后,是否有一些函数,对象,变量或任何我可以检查以查看polyfill是否有效的东西?

我希望能够检测到失败,然后重定向到带有“请升级”消息的页面。

对我来说,唯一的选择是在我的后端实现某种浏览器检测中间件,由于各种内部原因,我宁愿在此时避免这种中间件(并且因为它意味着特别列入白名单/列入黑名单的浏览器,这将变得乏味快速。)

提前谢谢。

1 个答案:

答案 0 :(得分:4)

简答:

快速测试:Firefox 38.0.5警告"否",而Chrome 44.0.2403.130 m警报"是"



function supportsPolymer() {
  return 'content' in document.createElement('template') && 'import' in document.createElement('link') && 'registerElement' in document && document.head.createShadowRoot;
}

if(supportsPolymer()) {
  
  //Good to go
  alert("Yes");
  
} else {
  
  //Is not supported
  alert("No");
  
}




详细答案:

您必须在Polymer的网站上查看this list

  
      
  • 模板
  •   
  • HTML Imports
  •   
  • 自定义元素
  •   
  • Shadow DOM
  •   

必须支持这些功能:

function supportsTemplate() {
  return 'content' in document.createElement('template');
}

if (supportsTemplate()) {
  // Good to go!
} else {
  // Use old templating techniques or libraries.
}
function supportsImports() {
  return 'import' in document.createElement('link');
}

if (supportsImports()) {
  // Good to go!
} else {
  // Use other libraries/require systems to load files.
}
function supportsCustomElements() {
  return 'registerElement' in document;
}

if (supportsCustomElements()) {
  // Good to go!
} else {
  // Use other libraries to create components.
}

How to check if a browser supports shadow DOM

if(document.head.createShadowRoot) {
    // I can shadow DOM
} else {
    // I can't
}

在一个功能中:

 function supportsShadowDom() {
   return document.head.createShadowRoot;
 }

以前代码段样式的未经测试的版本:

 function supportsShadowDom() {
   return 'createShadowRoot' in document;
 }

好的,在您实施了所有功能后,您可以执行以下操作:

 if (supportsCustomElements() && supportsTemplate() && supportsImports() && supportsShadowDom()) {
   // Good to go!
 } else {
   // Use other libraries to create components.
 }

这是https://github.com/WebComponents/webcomponentsjs#browser-support的当前矩阵:



<table><thead>
<tr>
<th>Polyfill</th>
<th align="center">IE10</th>
<th align="center">IE11+</th>
<th align="center">Chrome*</th>
<th align="center">Firefox*</th>
<th align="center">Safari 7+*</th>
<th align="center">Chrome Android*</th>
<th align="center">Mobile Safari*</th>
</tr>
</thead><tbody>
<tr>
<td>Custom Elements</td>
<td align="center">~</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
</tr>
<tr>
<td>HTML Imports</td>
<td align="center">~</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
</tr>
<tr>
<td>Shadow DOM</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
</tr>
<tr>
<td>Templates</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
</tr>
</tbody></table>
&#13;
&#13;
&#13;

这也可能很有趣: https://github.com/webcomponents/webcomponentsjs/issues/26

相关问题