Copy Text to Clipboard- IE, Firefox and Opera
January 11, 2008 – 3:09 amUpdated on: 02 August, 2008 at 11:56 PM IST
Recently I was working on Opera widget and got this problem of how to copy text to clipboard.
Though separate solution exists for IE and firefox but there is no exclusive solution for Opera
. But a single workaround do exists ![]()
Here are some ways to get the things work for the three browsers.
For All Web Browsers:
Method 1:
This method uses flash hack to copy the text to clipboard.
- The text to be copied is passed to a JavaScript function (named copyToClipboard) that takes the text as arguement.
- This function then embeds a flash file and uses it to copy the Text to clipboard.
- Download the files from HERE and check then yourself.
For Internet Explorer :
Method 1:
It’s the simplest to copy text to clipboard in IE because it’s a MS product enjoying the luxuries of MS windows.
- There is this one small statement that set the data on clipboard
- window.clipboardData.setData(”Text”,datatocopy);
- Download the file from HERE
For Firefox :
Method 1:
- This approach uses clipboard service to transfer data using native OS clipboard.
- Download the file HERE
- Though this approach gives a warning dialog box to the user.
Still the approach for the widget am developing is not working (Opera developers should provide certain privileges to widgets)
Chech out this video (Curious George OST) by Jack Johnson:
lol













5 Responses to “Copy Text to Clipboard- IE, Firefox and Opera”
Just what i was looking for. Thanks for the info
By DavidTan on Sat 09th Feb, 2008
ok..this works. thanks very much. i like the one for firefox, but how can i copy a selected text? i use phpbb3 and i want to copy the selected text from the code boxes.
function selectCode(a)
{
// Get ID of code block
var e = a.parentNode.parentNode.getElementsByTagName(’CODE’)[0];
// Not IE
if (window.getSelection)
{
var s = window.getSelection();
// Safari
if (s.setBaseAndExtent)
{
s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
}
// Firefox and Opera
else
{
var r = document.createRange();
r.selectNodeContents(e);
s.removeAllRanges();
s.addRange(r);
}
}
// Some older browsers
else if (document.getSelection)
{
var s = document.getSelection();
var r = document.createRange();
r.selectNodeContents(e);
s.removeAllRanges();
s.addRange(r);
}
// IE
else if (document.selection)
{
var r = document.body.createTextRange();
r.moveToElementText(e);
r.select();
}
}
and it’s called like this href=”#” onclick=”selectCode(this); return false;”
how can i make one of the methods that copy text to clipboard to copy the selected text?
By Skyler on Tue 12th Feb, 2008
I am not able to find code for Opera
By Kishor on Fri 29th Feb, 2008
Nice work, but its not work without flash player. how can we perform this task without using flash player.
By Shujahat Samandar on Fri 07th Mar, 2008