What is the compatibility with browsers when using the template tag?

1

Reference: link

Example usage:

Javascript:

window.onload = function() {
var contentHTML = document.getElementById("teste").content;
  var linha = document.importNode(contentHTML, true);
   document.getElementById("tabela").appendChild(linha);
}();

HTML:

<table id="tabela" class="table" style="width: 820px;">
<!-- aqui entra a templae -->
</table>

<!-- template -->
<template id="teste">
<caption>Optional table caption.</caption> <thead> <tr> <th>#</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td>Mark</td> <td>Otto</td> <td>@mdo</td> </tr> <tr> <th scope="row">2</th> <td>Jacob</td> <td>Thornton</td> <td>@fat</td> </tr> <tr> <th scope="row">3</th> <td>Larrya</td> <td>the Bird</td> <td>@twitter</td> </tr> </tbody>
</template>

JSFiddle Example

    
asked by anonymous 30.06.2016 / 17:41

1 answer

2

Ivan, according to the site Can I Use the template tag is not supported by IE , Opera Mini Android 4.3 .

But for these cases, you can use a Polyfill, as implemented by jeffcarp .:

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

  var templates = document.getElementsByTagName('template');
  var plateLen = templates.length;

  for (var x = 0; x < plateLen; ++x) {
    var template = templates[x];
    var content = template.childNodes;
    var fragment = document.createDocumentFragment();

    while (content[0]) {
      fragment.appendChild(content[0]);
    }

    template.content = fragment;
  }
})(); 
    
30.06.2016 / 17:52