I'm looking for a simple option to disallow any HTML entry. I want to prevent users from copy + pasting external HTML they find on the web as it always causes issues.
1 Answer
This is a dirty job to do cross browser. For safari and chrome you could use the paste event. For example:
document.querySelector("div[contenteditable]").addEventListener("paste", function(e) {
e.preventDefault();
var text = e.clipboardData.getData("text/plain");
var temp = document.createElement("div");
temp.innerHTML = text;
document.execCommand("insertHTML", false, temp.textContent);
});
Same is possible with IE using onbeforepaste and/or onpaste. But the major problem is, that you can't access the clipboard in firefox. Some projects work with a hidden textarea and capture keyboard combinations like CTRL+V but as far as I know these solutions don't work very satisfying.