Reputation: 81
For some reason in my javascript the window.location.href returns only the domain name and not the current page address.
function addLink() {
console.log('hey');
var body_element = document.getElementsByTagName('body')[0];
var selection;
selection = window.getSelection();
// added lines begin
var htmlDiv = document.createElement("div");
for (var i = 0; i < selection.rangeCount; ++i) {
htmlDiv.appendChild(selection.getRangeAt(i).cloneContents());
}
var selectionHTML = htmlDiv.innerHTML;
// added lines end
var pagelink = "<br /><br />Read more: " + window.location.href + " <br />";
var copytext = selectionHTML + pagelink; // <------------------- changed line
var newdiv = document.createElement('div');
newdiv.style.position='absolute';
newdiv.style.left='-99999px';
body_element.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function () {
body_element.removeChild(newdiv);
}, 0);
}
document.oncopy = addLink;
The file is active on http://www.chefsuccess.com thanks!
Upvotes: 2
Views: 422
Reputation: 6364
My script won't work in IE (even 9)? simple javascript to amend copy text
Perhaps you could find answer on above link.
IE needs
document.body.oncopy=copyCopyright
added to your onload event. (body doesn’t exist until loaded)
Upvotes: 2