admin管理员组

文章数量:1332352

I'm taking html content out of my database and displaying it on a page.

Unfortunately these pages often have unclosed html tags and cause problems later when the page is rendered.

I was wondering if there is a JavaScript implementation of something like tidy or htmlpurifier.

Basically some software which can preferably close html tags in a string.

Edit: I'm not in a browser environment (node.js)

I'm taking html content out of my database and displaying it on a page.

Unfortunately these pages often have unclosed html tags and cause problems later when the page is rendered.

I was wondering if there is a JavaScript implementation of something like tidy or htmlpurifier.

Basically some software which can preferably close html tags in a string.

Edit: I'm not in a browser environment (node.js)

Share Improve this question asked Jan 19, 2012 at 21:18 AaronAaron 1,58311 silver badges10 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

As you tagged your question with node.js:

npm install tidy

Something like this could work:

function tidy(htmldata) {
    var d = document.createElement('div');
    d.innerHTML = htmldata;
    return d.innerHTML;
}

http://code.google./p/google-caja/wiki/JsHtmlSanitizer balances tags.

本文标签: How to close html tags in JavaScriptStack Overflow