views:

317

answers:

7

Facebook's status update input (well, contenteditable div) detects links.

When typing a link it waits until the spacebar is pressed before fetching the URL.

When pasting a link it fetches the URL instantly.

I can already parse the url after the spacebar is pressed...but I'm not sure about detecting when content is pasted.

Any solution would be awesome; a jQuery formatted solution will be BEST!

A: 

Check the field at half-second increments. If you detect a large change in the amount of text, it has probably been pasted.

Jamie
Using timeout()? Events are clearly better for this.
Georg
+4  A: 

Listen to the keyup event in the field. If the field's content has changed by more than 1 character after one keyup, something has been pasted.

As @epascarello points out, check right click events, too, as the user could be using the context menu.

Pekka
duh. makes sense. my brain is fried today. I'll try this in a bit and get back to you. Thanks!
David Murdoch
How about right click and paste OR view and paste. key events are only part of the equation.
epascarello
Good point. Right click needs to be in, too.
Pekka
Just checking right click isn't good enough, the click on the actual context menu item won't fire the right click event, so you'd be firing too early.
Andy E
Also, what about the these keys in this order `alt->E->P`?
Andy E
onpaste event works perfectly. This method would requires a key being pressed before working. Thanks for the tip, though.
David Murdoch
Didn't know about onpaste - good to know. Thanks for letting me know.
Pekka
+2  A: 

Compare successive onChange events. If the difference between them is more than one character, it's a paste. Else it's typed in.

Rakesh Pai
The onChange event only fires after a `blur` of the textarea, so this wouldn't work.
Alex Sexton
A: 

or use the keypress event to detect a CTRL+V

adam
Thats assuming too much about which OS is being used.
Robert Jeppesen
Juan
A: 
 <script type="text/javascript">
  $(document).ready(function(){
   $("#text").keypress (function(e){
    var code = (e.keyCode ? e.keyCode : e.which);
    if ((code == 86 || code == 118) && e.ctrlKey) //86 = v 118 = V
    {
     alert("Pasted!");
    }   
   });
  });
 </script>

</head>
<body>
 <textarea id="text">
 </textarea>
</body>
</html>
Tim Santeford
+7  A: 

Modern day browsers support onpaste

epascarello
it's so obvious... why is everyone else overcomplicating this in their answers?
Andy E
My gosh I feel like an idiot now. msdn says its been there since version 5! Thanks, I'm away from my dev machine but I'll let you know how well it works. Thanks for the obvious!
David Murdoch
+1  A: 

I am actually going to suggest it listens to every keyup because it has multiple uses, if you type @ it will suggest friends, etc.

It probably scans the text and finds links and makes them, well linkable, and then crawls the page so you can post it as "Sharing" the page.

Garrett
Didn't know about the @ sign. +1 for that.
David Murdoch