How can one possibly sanitize a HTML DOM Tree from all JavaScript occurrences, meaning: on(click|mouseover|etc)
, href:javascript...
, <script>
and all other possible variants of (inline) JavaScript, while using JavaScript?
For example: I want users to upload their HTML file, copy the contents in the <body>
tags and insert it into one of my pages. I don't want to allow JavaScript in their HTML files. I could use <iframe sandbox>
, but I wondered whether there is another way.
The following uses the Element.attributes collection to remove inline
on
handlers and attributes containing the word, "javascript" – without affecting other DOM attributes.I don't think there's a way to remove
script
elements before they've had a chance to run.