code_junkie

Why does IE give unexpected errors when setting innerHTML

Nov 11th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. // removing the scripts to avoid any 'Permission Denied' errors in IE
  2. var cleaned = html.replace(/<script(.|s)*?/script>/g, "");
  3.  
  4. // IE is stricter on malformed HTML injecting direct into DOM. By injecting into
  5. // an element that's not yet part of DOM it's more lenient and will clean it up.
  6. if (jQuery.browser.msie)
  7. {
  8. var tempElement = document.createElement("DIV");
  9. tempElement.innerHTML = cleaned;
  10. cleaned = tempElement.innerHTML;
  11. tempElement = null;
  12. }
  13. // now 'cleaned' is ready to use...
  14.  
  15. replaceInReadOnly(document.getElementById("links"), "<a href>........etc</a>");
  16.  
  17. function replaceInReadOnly(element, content){
  18. var newNode = document.createElement();
  19. newNode.innerHTML = content;
  20. var oldNode = element.firstChild;
  21. var output = element.replaceChild(newNode, oldNode);
  22. }
Add Comment
Please, Sign In to add comment