I wanted to use jQuery to tweak the copy-mechanism. I noticed that when the anchor and focus nodes of a selection were the same, the corresponding jQuery objects (compared using the .is method) were not.
In Chrome and Firefox (Windows) the comparison fails in this example:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(function () {
$("#testdiv").on("copy", function(event) {
var sel = window.getSelection();
var $anchorNode = $(sel.anchorNode);
var $focusNode = $(sel.focusNode);
$("#logdiv").append("<p>The DOM nodes are" + (sel.anchorNode == sel.focusNode ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>The jQuery objects are" + ($anchorNode.is($focusNode) ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>Parent: the DOM nodes are" + (sel.anchorNode.parent == sel.focusNode.parent ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>Parent: the jQuery objects are" + ($anchorNode.parent().is($focusNode.parent() ) ? "" : "<b> not</b>") + " the same.</p>");
});
});
</script>
</head>
<body>
<div id="testdiv">
Select some text inside this div and press Ctrl+C.
</div>
<div id="logdiv" style="border: 1pt solid black;"><p><i>Log</i></p></div>
</body>
</html>
I prepared a jsFiddle to illustrate this, but couldn't reproduce it there.
So I tried with SO's snippet widget. It works again:
$(function () {
$("#testdiv").on("copy", function(event) {
var sel = window.getSelection();
var $anchorNode = $(sel.anchorNode);
var $focusNode = $(sel.focusNode);
$("#logdiv").append("<p>The DOM nodes are" + (sel.anchorNode == sel.focusNode ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>The jQuery objects are" + ($anchorNode.is($focusNode) ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>Parent: the DOM nodes are" + (sel.anchorNode.parent == sel.focusNode.parent ? "" : "<b> not</b>") + " the same.</p>");
$("#logdiv").append("<p>Parent: the jQuery objects are" + ($anchorNode.parent().is($focusNode.parent() ) ? "" : "<b> not</b>") + " the same.</p>");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="testdiv">
Select some text inside this div and press Ctrl+C.
</div>
<div id="logdiv" style="border: 1pt solid black;"><p><i>Log</i></p></div>
Any ideas on what's happening?
<script>above (like i.e: pagehead) ... jsfiddle.net/sh2ke2a8/2 - Strange, your code already uses theDOM readyshorthand but anyways it fails if placed in<head>