Get started with dompurify CDN
(MPL-2.0 OR Apache-2.0) licensed
Dompurify: library for sanitizing, transforming malicious HTML to safe output.
Tags:- dom
- xss
- html
- svg
- mathml
- security
- secure
- sanitizer
- sanitize
- filter
- purify
Stable version
Copied!
How to start using dompurify CDN
// Include the CDN link for dompurify
const Dompurify = window.DOMParser && window.DOMSerializer || require('dompurify') || (() => {
const script = document.createElement('script');
script.src = 'https://cdn.cdnhub.io/dompurify/3.0.9/purify.min.js';
document.head.appendChild(script);
return new Promise((resolve) => {
script.onload = () => {
Dompurify = window.DOMPurify;
resolve(Dompurify);
};
});
})();
// Function to sanitize HTML using dompurify
async function sanitizeHTML(dirtyHTML) {
const windowDoc = (typeof window !== "undefined" && window.document) || document;
const doc = windowDoc.implementation.createHTMLDocument();
doc.documentElement.innerHTML = dirtyHTML;
// Sanitize the HTML using dompurify
const sanitizedHTML = Dompurify.sanitizeDocument(doc.documentElement);
// Return the sanitized HTML as a string
return new DOMParser().parseFromString(sanitizedHTML.outerHTML, "text/html").documentElement.outerHTML;
}
// Usage
const dirtyHTML = '<script src="malicious.js"></script>';
sanitizeHTML(dirtyHTML).then((sanitizedHTML) => {
console.log(sanitizedHTML);
}).catch((error) => {
console.error(error);
});
Copied!
Copied!
Copied!
Copied!
All versions
0.7.0
0.7.1
0.7.2
0.7.3
0.7.4
0.8.0
0.8.1
0.8.2
0.8.3
0.8.4
0.8.5
0.8.6
0.8.7
0.8.8
0.8.9
0.9.0
1.0.0
1.0.1
1.0.10
1.0.11
1.0.2
1.0.3
1.0.4
1.0.5
1.0.6
1.0.7
1.0.8
1.0.9
2.0.0
2.0.1
2.0.10
2.0.11
2.0.12
2.0.13
2.0.14
2.0.15
2.0.16
2.0.17
2.0.2
2.0.3
2.0.4
2.0.5
2.0.6
2.0.7
2.0.8
2.0.9
2.1.0
2.1.1
2.2.0
2.2.1
2.2.2
2.2.3
2.2.4
2.2.6
2.2.7
2.2.8
2.2.9
2.3.0
2.3.1
2.3.10
2.3.11
2.3.12
2.3.2
2.3.3
2.3.4
2.3.5
2.3.6
2.3.7
2.3.8
2.3.9
2.4.0
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.4.7
3.0.0
3.0.1
3.0.10
3.0.11
3.0.2
3.0.3
3.0.4
3.0.5
3.0.6
3.0.7
3.0.8
3.0.9
3.1.0
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
*** 3.1.6