简体   繁体   中英

My script won't work in IE (even 9)? simple javascript to amend copy text

So I was browsing the internet one day, and copied a chunk of text " my cool text " and pasted it to facebook, only to see that it changed it to " my cool text - Read More at URL ", I was in awe! That's awesome!

So I dove in a little and found some tutorials and such. I took to my own to convert it to a customizable plugin with dozens of options, and it outputs this (or similar based on options):

function copyCopyright() {
    var theBody = document.getElementsByTagName("body")[0];
    var selection;
    selection = window.getSelection();
    var copyrightLink = '<br /><br />Read more at: '+document.location.href+'<br /> &copy;2012  ';
    var copytext = selection + copyrightLink;
    var extraDiv = document.createElement("div");
    extraDiv.style.position="absolute";
    extraDiv.style.left="-99999px";
    theBody.appendChild(extraDiv);
    extraDiv.innerHTML = copytext;
    selection.selectAllChildren(extraDiv);
    window.setTimeout(function() {
        theBody.removeChild(extraDiv);
    },0);
}
document.oncopy = copyCopyright;​

works GREAT in Chrome and Firefox, etc. But of COURSE it doesn't work in IE (even IE9!). I'm fairly new to Javascript, especially hunting down IE problems with it.

Is there a function or method or something above that IE just won't recognize that I'll have to find an alternate way around?

IE needs

document.body.oncopy=copyCopyright

added to your onload event. (body doesn't exist until loaded)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM