How Does Facebook Detect When A Link As Been PASTED
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
Solution 1:
Modern day browsers support onpaste:
<!DOCTYPE html>
<html>
<head>
<title>onpaste event example</title>
</head>
<body>
<h1>Play with this editor!</h1>
<textarea id="editor" rows="3" cols="80">
Try pasting text into this area!
</textarea>
<script>
function log(txt) {
document.getElementById("log").appendChild(document.createTextNode(txt + "\n"));
}
function pasteIntercept(evt) {
log("Pasting!");
}
document.getElementById("editor").addEventListener("paste", pasteIntercept, false);
</script>
<h2>Log</h2>
<textarea rows="15" cols="80" id="log" readonly="true"></textarea>
</body>
</html>
Solution 2:
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.
Solution 3:
Compare successive onChange events. If the difference between them is more than one character, it's a paste. Else it's typed in.
Solution 4:
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.
Solution 5:
<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>
Post a Comment for "How Does Facebook Detect When A Link As Been PASTED"