views:

1675

answers:

2

Hi Guys,

I need to copy a text from a textbox into the clipboard with ASP.NET, I want a code that is compatable with Mozilla Firefox and IE? Have you any idea?

Thanks Alot!

+2  A: 

Internet Explorer clipboard copy is trivial:

// set the clipboard
var x = 'Whatever you want on the clipboard';
window.clipboardData.setData('Text',x);

// get the clipboard data
window.clipboardData.getData('Text');

Firefox, not trivial at all. Impossible actually with pure JS unless you have signed scripts etc. There is a workaround using a Flash object, however. Read about it here

Strelok
A: 

For non-IE browsers use this common-all-over-the-world-used script. Google for "_clipboard.swf" file. (though, this code will NOT work on newest Flash 10 becouse of security reasons)

var flashcopier = 'flashcopier';

     if(!document.getElementById(flashcopier)) {
        var divholder = document.createElement('div');
        divholder.id = flashcopier;
        document.body.appendChild(divholder);
     }

     document.getElementById(flashcopier).innerHTML = '';
     var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent('YOUR_VALUE_FOR_CLIPBOARD')+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
     document.getElementById(flashcopier).innerHTML = divinfo;