RSS2.0

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

Ask questions related to online shopping in India

Where to buy online in India?

wheretobuyonline.in

Copy to Clipboard

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

 window.clipboardData.setData("Text",text2copy);
But this will work only in Internet Explorer. So what will you for other browsers? Here is another solution.
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var clip = Components.classes['@mozilla.org/widget/clipboard;1']
.createInstance(Components.interfaces.nsIClipboard);
if (!clip) return;
var trans = Components.classes['@mozilla.org/widget/transferable;1']
.createInstance(Components.interfaces.nsITransferable);
if (!trans) return;

trans.addDataFlavor('text/unicode');

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

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

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

str.data = 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 "http://loveorkut.com/" 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: http://rapidshare.com/files/81866988...board.swf.html
Keep it in the same folder.

Here is the code:

function copy(text2copy) {
if (window.clipboardData) {
window.clipboardData.setData("Text",text2copy);
} else {
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 = '';
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

0 comments:

My Shared Items in Google Reader

My Blogroll

Followers

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