0

I'm building a custom right-click menu for my system and I need to know how can I make a JavaScript function to copy the selected text, basically 100% like the original right-click menu does.

I'm aware of the Flash work-arounds. I want to do this in JavaScript.

Every answer I've seen so far is only a half-answer because none of them explains how to make a copy button for the selected text - all what they do is copy a pre-defined text or a text from a textbox.

random
  • 9,774
  • 10
  • 66
  • 83
Ricardo
  • 1,653
  • 8
  • 26
  • 51
  • 1
    That's exactly the issue--there isn't a cross-browser method to do this. – Purag Aug 08 '11 at 19:14
  • yes, but you can use little flash part to do that.. just google for it (examle http://www.deluxeblogtips.com/2010/06/javascript-copy-to-clipboard.html).. – slobodan Aug 08 '11 at 19:23

4 Answers4

1

Modern Day Browsers block access to the clipboard. The user has to have the security setting correct.

There are flash work-arounds, but they are not the best.

epascarello
  • 204,599
  • 20
  • 195
  • 236
1

For non-IE browsers you will most likely have to use a flash solution. For IE, however, this method works perfectly:

function copyToClipboard(s) {           //only works in IE :(
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData('text', s);
    }
}
Maxx
  • 3,925
  • 8
  • 33
  • 38
  • well the thing is - i need it to copy exactly the text they selected, so all the answers i got here so far are only half-answers because i need my users to chose what text they want to copy and not what the current script does (copy a text that I write). It's not hard to go into Goggle and find the exact same thing as you gave below, thanks for the answer anyway ;) – Ricardo Aug 08 '11 at 19:36
0

no idea if this will work, but a google search yielded:

function getSel(){
  var w=window,d=document,gS='getSelection';
  return (''+(w[gS]?w[gS]():d[gS]?d[gS]):d.selection.createRange().text)).replace(/(^\s+|\s+$)/g,'');
}

http://snippets.dzone.com/posts/show/2914

wolv2012
  • 429
  • 3
  • 8
0

A workable cross-browser approach (minus iOS) would be to use ExternalInterface and setClipboard. So you would have a swf, flash file, that only listens to a function you call from Javascript to set the clipBoard.

Joe
  • 80,724
  • 18
  • 127
  • 145