简体   繁体   English

如何检查浏览器是否支持Polymer?

[英]How to check if a browser supports Polymer?

我如何检查(JS或HTML代码)当前浏览器是否支持Polymer?

Short answer: 简短回答:

Quick test: Firefox 38.0.5 alerts "No", while Chrome 44.0.2403.130 m alerts "Yes" 快速测试: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"); } 

Detailed answer: 详细解答:

You've to check this list on Polymer's website. 您需要在Polymer的网站上查看此列表

  • Template 模板
  • HTML Imports HTML导入
  • Custom Elements 自定义元素
  • Shadow DOM 影子DOM

These features have to be supported: 必须支持这些功能:

 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 如何检查浏览器是否支持shadow DOM

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

In a function: 在一个功能:

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

Untested version in the style of the previous snippets: 以前片段样式的未经测试的版本:

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

Okay, after you've implemented every function you can do something like this: 好的,在您实现了每个功能之后,您可以执行以下操作:

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

This is the current matrix from https://github.com/WebComponents/webcomponentsjs#browser-support : 这是来自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> 

This may be interesting, too: https://github.com/webcomponents/webcomponentsjs/issues/26 这也可能很有趣: https//github.com/webcomponents/webcomponentsjs/issues/26

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM