Posted By

nicolaspar on 05/05/10


Tagged

ie error innerHTML parser innerXHTML ieInnerHtml propieties


Versions (?)

innerXHTML


 / Published in: JavaScript
 

html = innerXHTML (document.getElementById('div_id')) alert(html)

  1. innerXHTML = function($source,$string,$appendage) {
  2. // (v0.4) Written 2006 by Steve Tucker, http://www.stevetucker.co.uk
  3. if (typeof($source) == 'string') $source = document.getElementById($source);
  4. if (!($source.nodeType == 1)) return false;
  5. var $children = $source.childNodes;
  6. var $xhtml = '';
  7. if (!$string) {
  8. for (var $i=0; $i<$children.length; $i++) {
  9. if ($children[$i].nodeType == 3) {
  10. var $text_content = $children[$i].nodeValue;
  11. $text_content = $text_content.replace(/</g,'&lt;');
  12. $text_content = $text_content.replace(/>/g,'&gt;');
  13. $xhtml += $text_content;
  14. }
  15. else if ($children[$i].nodeType == 8) {
  16. $xhtml += '<!--'+$children[$i].nodeValue+'-->';
  17. }
  18. else {
  19. $xhtml += '<'+$children[$i].nodeName.toLowerCase();
  20. var $attributes = $children[$i].attributes;
  21. for (var $j=0; $j<$attributes.length; $j++) {
  22. var $attName = $attributes[$j].nodeName.toLowerCase();
  23. var $attValue = $attributes[$j].nodeValue;
  24. if ($attName == 'style' && $children[$i].style.cssText) {
  25. $xhtml += ' style="'+$children[$i].style.cssText.toLowerCase()+'"';
  26. }
  27. else if ($attValue && $attName != 'contenteditable') {
  28. $xhtml += ' '+$attName+'="'+$attValue+'"';
  29. }
  30. }
  31. $xhtml += '>'+innerXHTML($children[$i]);
  32. $xhtml += '</'+$children[$i].nodeName.toLowerCase()+'>';
  33. }
  34. }
  35. }
  36. else {
  37. if (!$appendage) {
  38. while ($children.length>0) {
  39. $source.removeChild($children[0]);
  40. }
  41. $appendage = false;
  42. }
  43. $xhtml = $string;
  44. while ($string) {
  45. var $returned = translateXHTML($string);
  46. var $elements = $returned[0];
  47. $string = $returned[1];
  48. if ($elements) {
  49. if (typeof($appendage) == 'string') $appendage = document.getElementById($appendage);
  50. if (!($appendage.nodeType == 1)) $source.appendChild($elements);
  51. else $source.insertBefore($elements,$appendage);
  52. }
  53. }
  54. }
  55. return $xhtml;
  56. }
  57. function translateXHTML($string) {
  58. var $match = /^<\/[a-z0-9]{1,}>/i.test($string);
  59. if ($match) {
  60. var $return = Array;
  61. $return[0] = false;
  62. $return[1] = $string.replace(/^<\/[a-z0-9]{1,}>/i,'');
  63. return $return;
  64. }
  65. $match = /^<[a-z]{1,}/i.test($string);
  66. if ($match) {
  67. $string = $string.replace(/^</,'');
  68. var $element = $string.match(/[a-z0-9]{1,}/i);
  69. if ($element) {
  70. var $new_element = document.createElement($element[0]);
  71. $string = $string.replace(/[a-z0-9]{1,}/i,'');
  72. var $attribute = true;
  73. while ($attribute) {
  74. $string = $string.replace(/^\s{1,}/,'');
  75. $attribute = $string.match(/^[a-z1-9_-]{1,}="[^"]{0,}"/i);
  76. if ($attribute) {
  77. $attribute = $attribute[0];
  78. $string = $string.replace(/^[a-z1-9_-]{1,}="[^"]{0,}"/i,'');
  79. var $attName = $attribute.match(/^[a-z1-9_-]{1,}/i);
  80. $attribute = $attribute.replace(/^[a-z1-9_-]{1,}="/i,'');
  81. $attribute = $attribute.replace(/;{0,1}"$/,'');
  82. if ($attribute) {
  83. var $attValue = $attribute;
  84. if ($attName == 'value') $new_element.value = $attValue;
  85. else if ($attName == 'class') $new_element.className = $attValue;
  86. else if ($attName == 'style') {
  87. var $style = $attValue.split(';');
  88. for (var $i=0; $i<$style.length; $i++) {
  89. var $this_style = $style[$i].split(':');
  90. $this_style[0] = $this_style[0].toLowerCase().replace(/(^\s{0,})|(\s{0,1}$)/,'');
  91. $this_style[1] = $this_style[1].toLowerCase().replace(/(^\s{0,})|(\s{0,1}$)/,'');
  92. if (/-{1,}/g.test($this_style[0])) {
  93. var $this_style_words = $this_style[0].split(/-/g);
  94. $this_style[0] = '';
  95. for (var $j=0; $j<$this_style_words.length; $j++) {
  96. if ($j==0) {
  97. $this_style[0] = $this_style_words[0];
  98. continue;
  99. }
  100. var $first_letter = $this_style_words[$j].toUpperCase().match(/^[a-z]{1,1}/i);
  101. $this_style[0] += $first_letter+$this_style_words[$j].replace(/^[a-z]{1,1}/,'');
  102. }
  103. }
  104. $new_element.style[$this_style[0]] = $this_style[1];
  105. }
  106. }
  107. else if (/^on/.test($attName)) $new_element[$attName] = function() { eval($attValue) };
  108. else $new_element.setAttribute($attName,$attValue);
  109. }
  110. else $attribute = true;
  111. }
  112. }
  113. $match = /^>/.test($string);
  114. if ($match) {
  115. $string = $string.replace(/^>/,'');
  116. var $child = true;
  117. while ($child) {
  118. var $returned = translateXHTML($string,false);
  119. $child = $returned[0];
  120. if ($child) $new_element.appendChild($child);
  121. $string = $returned[1];
  122. }
  123. }
  124. $string = $string.replace(/^\/>/,'');
  125. }
  126. }
  127. $match = /^[^<>]{1,}/i.test($string);
  128. if ($match && !$new_element) {
  129. var $text_content = $string.match(/^[^<>]{1,}/i)[0];
  130. $text_content = $text_content.replace(/&lt;/g,'<');
  131. $text_content = $text_content.replace(/&gt;/g,'>');
  132. var $new_element = document.createTextNode($text_content);
  133. $string = $string.replace(/^[^<>]{1,}/i,'');
  134. }
  135. $match = /^<!--[^<>]{1,}-->/i.test($string);
  136. if ($match && !$new_element) {
  137. if (document.createComment) {
  138. $string = $string.replace(/^<!--/i,'');
  139. var $text_content = $string.match(/^[^<>]{0,}-->{1,}/i);
  140. $text_content = $text_content[0].replace(/-->{1,1}$/,'');
  141. var $new_element = document.createComment($text_content);
  142. $string = $string.replace(/^[^<>]{1,}-->/i,'');
  143. }
  144. else $string = $string.replace(/^<!--[^<>]{1,}-->/i,'');
  145. }
  146. var $return = Array;
  147. $return[0] = $new_element;
  148. $return[1] = $string;
  149. return $return;
  150. }

Report this snippet  

You need to login to post a comment.