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

Asked

Viewed 76 times

1

Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template

Example of use:

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

1 answer

2


Ivan, according to the website Can I Use the template tag is not supported by IE, Opera Mini and the 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;
  }
})(); 
  • thanks for the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.