html5 / jQuery innerHTML fix for IE


/ Published in: JavaScript
Save to your folder(s)

If you try to access innerHTML of HTML5 elements like or or even just the #foo id then IE returns nothing
innerShiv solves this.


Copy this code and paste it in your HTML
  1. // http://jdbartlett.github.com/innershiv | WTFPL License
  2. window.innerShiv = (function() {
  3. var d, r;
  4.  
  5. return function(h, u) {
  6. if (!d) {
  7. d = document.createElement('div');
  8. r = document.createDocumentFragment();
  9. /*@cc_on d.style.display = 'none';@*/
  10. }
  11.  
  12. var e = d.cloneNode(true);
  13. /*@cc_on document.body.appendChild(e);@*/
  14. e.innerHTML = h.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  15. /*@cc_on document.body.removeChild(e);@*/
  16.  
  17. if (u === false) return e.childNodes;
  18.  
  19. var f = r.cloneNode(true), i = e.childNodes.length;
  20. while (i--) f.appendChild(e.firstChild);
  21.  
  22. return f;
  23. }
  24. }());

URL: http://jdbartlett.github.com/innershiv

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.