Catching and Cleaning Pasted Text Cross-Browser
Pasting text into web applications often poses a concern: retaining formatting while filtering pasted data. This article explores a cross-browser solution to this challenge.
Solution 1: Plain Text Support for Modern Browsers
For IE6 , Firefox 22 , Chrome, Safari, and Edge, the following code snippet can be employed to capture and process pasted text:
function handlePaste(e) { var clipboardData, pastedData; // Stop data actually being pasted into div e.stopPropagation(); e.preventDefault(); // Get pasted data via clipboard API clipboardData = e.clipboardData || window.clipboardData; pastedData = clipboardData.getData('Text'); // Do whatever with pasteddata alert(pastedData); } document.getElementById('editableDiv').addEventListener('paste', handlePaste);
This approach extracts plain text from the clipboard and allows further processing, ensuring that original formatting in the target element remains unaffected.
The above is the detailed content of How Can I Capture and Clean Pasted Text Across Different Browsers?. For more information, please follow other related articles on the PHP Chinese website!