I moved to Now I am blogging there, so please visit and keep me reading. love, Debiprasad

Ask questions related to online shopping in India

Where to buy online in India?

Copy to Clipboard

Do you want to copy any contents on click of button or any other event. Here is the solution.

But this will work only in Internet Explorer. So what will you for other browsers? Here is another solution.'UniversalXPConnect');
var clip = Components.classes[';1']
if (!clip) return;
var trans = Components.classes[';1']
if (!trans) return;


var str = new Object();
var len = new Object();

var str = Components.classes[";1"]

var copytext = "The Text you want to copy"; = copytext;

trans.setTransferData("text/unicode", str, copytext.length*2);

var clipid=Components.interfaces.nsIClipboard;

if (!clip) return false;

clip.setData(trans, null, clipid.kGlobalClipboard);

And this may show this error
uncaught exception: A script from "" was denied UniversalXPConnect Privilages.

This is because, Mozilla doesn't allow copying to clipboard unless the script is signed (Digital signatures, from Verizon, etc.). And even if its signed it asks for user permission before allowing the script to do so.
So what will you do. Here is the solution:
There is this flash component which allows copying to clipboard from any browsers. Its not designed by me. But I had used it once and its working in almost all browsers I have tested. I am not getting the exact source of this file. But its released under the GPL, so freely distributable.

Download this file:
Keep it in the same folder.

Here is the code:

function copy(text2copy) {
if (window.clipboardData) {
} else {
var flashcopier = 'flashcopier';
if(!document.getElementById(flashcopier)) {
var divholder = document.createElement('div'); = flashcopier;
document.getElementById(flashcopier).innerHTML = '';
var divinfo = '';
document.getElementById(flashcopier).innerHTML = divinfo;

It has also some limitations, I will discuss it later. Please give your feedback and comments.

Copy to ClipboardSocialTwist Tell-a-Friend TwitThis Reading: Copy to ClipboardPost Link to Twitter


My Shared Items in Google Reader

My Blogroll


Want to see/show anything here? Contact me :)