0

Possible Duplicate:
HTML formatter/tidy/beautifier for JavaScript

I want to pass HTML code to a div:

myDiv.innerHTML = htmlCode;

its similar like I was trying to use an IFRAME. The problem is, if a htmlCode is not formatted well (there is a missing </div> for example) then it spoils all the page. How to clean those?

Community
  • 1
  • 1
John Smith
  • 283
  • 5
  • 12

2 Answers2

2

Take a look: google-caja — compiler for making third-party HTML, CSS and JavaScript safe for embedding

Dmitry Kurilo
  • 404
  • 4
  • 11
2

You could first use a DOMParser object to convert the HTML code to a DOM structure. That will fix any broken markup. You can then insert the resulting DOM structure as a child to your div.

Be careful, though: Embedding HTML code from an untrusted source is very dangerous, because it allows Cross Site Scripting attacks.

Philipp
  • 67,764
  • 9
  • 118
  • 153