JavaScript get clipboard data on paste event (Cross browser)

How can a web application detect a paste event and retrieve the data to be pasted?

I would like to remove HTML content before the text is pasted into a rich text editor.

Cleaning the text after being pasted afterwards works, but the problem is that all previous formatting is lost. For example, I can write a sentence in the editor and make it bold, but when I paste new text, all formatting is lost. I want to clean just the text that is pasted, and leave any previous formatting untouched.

Ideally, the solution should work across all modern browsers (e.g., MSIE, Gecko, Chrome, and Safari).

Note that MSIE has clipboardData.getData(), but I could not find similar functionality for other browsers.

22 Answers
22

Leave a Comment