Categories
JavaScript Answers

How to sanitize or rewrite HTML on the client side with JavaScript?

Spread the love

Sometimes, we want to sanitize or rewrite HTML on the client side with JavaScript.

In this article, we’ll look at how to sanitize or rewrite HTML on the client side with JavaScript.

How to sanitize or rewrite HTML on the client side with JavaScript?

To sanitize or rewrite HTML on the client side with JavaScript, we can use the sanitize-html package.

To install it, we run

npm install --save sanitize-html

Then we write

import sanitizeHtml from "sanitize-html";

//...

const sanitized = sanitizeHtml(htmlInput);

to import the sanitizeHtml function and call it with the htmlInput HTNL string to return a sanitized version of the string.

Conclusion

To sanitize or rewrite HTML on the client side with JavaScript, we can use the sanitize-html package.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *