javascript - Copying text to clipboard, firefox only, no plugins - Stack Overflow

I want to click a button a copy the text inside <div id="content"> into the clipboard.

I want to click a button a copy the text inside <div id="content"> into the clipboard. Is there a way do this with javascript or jquery, but without using a plugin. I don't need it to be cross-browser, just on Firefox.

$('#copy').click(function(){
   var cont = $('#content').text();
   //how to copy cont to clipboar?
});

I want to click a button a copy the text inside <div id="content"> into the clipboard. Is there a way do this with javascript or jquery, but without using a plugin. I don't need it to be cross-browser, just on Firefox.

$('#copy').click(function(){
   var cont = $('#content').text();
   //how to copy cont to clipboar?
});
Share Improve this question asked Sep 19, 2011 at 11:55 sameoldsameold 19.3k22 gold badges66 silver badges89 bronze badges 1
  • possible duplicate of How to Copy to Clipboard in JavaScript? – peirix Commented Sep 19, 2011 at 11:58
Add a ment  | 

4 Answers 4

Reset to default 1

Worked until about November 2012, then Mozilla destroyed it by an update. Now I've got a workaround: Open new window with contents to copy inside.

Thanks to Matthew Flaschen for the DataURL idea (https://stackoverflow./a/3665147/1120146)

/**
*   To use the clipboard from Mozilla / NS / Firefox:
*
*   Clipboard access works only up to Firefox 14 :-( (thanks to those security fanatics)
*
*   Solution for later versions: Window pops up with text inside (data url)
*   
*   In "about:config" : 
*       set signed.applets.codebase_principal_support = true!
*
*   @param text: The text which shold be copied to clipboard
*   @param fallbackContentType: The content type of the text, if clipboard access 
*                               doesn't work, i.e. "text/csv"
*                               default: text/plain
*/

function toClipboard(text, fallbackContentType) {
    var success = false;
    if (window.clipboardData)    {
           // the IE-manier
        window.clipboardData.setData("Text", text);
        success = true;
    }
    else if (windowscape)  {

        if(netscape.security.PrivilegeManager != undefined) {
            netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

            var clip = Components.classes['@mozilla/widget/clipboard;1'].getService(Components.interfaces.nsIClipboard);
            var trans = Components.classes['@mozilla/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);

            if(typeof(clip) == "object" && typeof(trans) == "object") {
                trans.addDataFlavor('text/unicode');

                var stingSupporter = Components.classes["@mozilla/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);

                stingSupporter.data = text;
                trans.setTransferData("text/unicode", stingSupporter, text.length * 2);
                var clipid = Components.interfaces.nsIClipboard;
                clip.setData(trans, null, clipid.kGlobalClipboard);

                success = true;
            }
        }
        else { // Firefox > v15
            // Create Data URL
            if(fallbackContentType == undefined) fallbackContentType = "text/plain";

            var url = "data:"+ fallbackContentType +"," + encodeURIComponent(text);
            window.open(url);
        }
    }
    return success;
}

You're going to need to use Flash for that. Read the following answers:

How do I copy to the clipboard in JavaScript?

Use Zero Clipboard. This is the best.

No, there's no way before HTML5. But implementation of that is even tricky. All the plugins use flash to copy to clipboard. You can use zClip http://www.steamdev./zclip/.

What gion_13 has said also requires flash as u can notice in the article that the link has. So it's no harm to use a tiny plugin to copy to clipboard :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745349080a4623717.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信