How can I find out what the user is typing or pasting?

In the text fields of my JSP, I want to know if the user is entering data or just pasting. How can I define this using javascript?

EDIT: According to Andy's answer, I know how I can do this, but still curious how these guys recorded the onpaste event .

+3
source share
4 answers

Safari, Chrome, Firefox and Internet Explorer support the event onpaste(not sure about Opera). Lock the event onpasteand you can catch it whenever something is inserted.


Writing this is simple enough. Add an event handler to your input using html:
<input type="text" id="myinput" onpaste="handlePaste(event);">

or JavaScript DOM:

var myInput = document.getElementById("myInput");

if ("onpaste" in myInput) // onpaste event is supported
{
    myInput.onpaste = function (e)
    {
        var event = e || window.event;
        alert("User pasted");
    }
}
// Check for mutation event support instead
else if(document.implementation.hasFeature('MutationEvents','2.0'))
{
    /* You could handle the DOMAttrModified event here, checking 
       new value length vs old value length but it wouldn't be 100% reliable */
}

From what I read, Opera does not support the event onpaste. You can use the event DOMAtrrModified, but it could fire even if the scripts change the value of the input field, so you need to be careful with it. Unfortunately, I am not familiar with mutation events, so I would not want to spoil this answer by writing an example that I would not be sure of.

+9
source

Count the keystroke and make sure it matches the value in the text box. The insert will not contain the full number of characters, as in a text field.

+1
source

. , , . ( ) , , . ( ).

0

, textarea onPaste.

HTML:

<textarea id="textEditor" />

JS:

var editor = document.getElementById("textEditor");

if (isIE /* determine this yourself */) {
   editor.onPaste = function() {

   }
} else {
   //Not IE
   editor.onpaste = function() {

   }
}

//The capitalisation of the onpaste (non-IE) and onPaste (IE) makes a difference.

, onKeyDown, onKeyUp, onKeyPress .

, .

, SO IE onPaste, javascript, HTML

0

Source: https://habr.com/ru/post/1736964/


All Articles