0

I have a textarea that includes information. It has an onfocus attribute which selects all text inside it automatically (onfocus="this.select();). I want that when I focus on the textarea, all the selected text get's automatically copied to my clipboard, so I can easily paste it anywhere without having to right click to copy or pressing CTRL + C. How would this be done?

aborted
  • 4,504
  • 11
  • 59
  • 118
  • Your question is answered here: http://stackoverflow.com/questions/5253299/copy-to-clipboard-in-javascript-having-hidden-input-field – Sudi Dec 12 '12 at 23:54

4 Answers4

2

In general, a browser's JavaScript engine does not have access to any of the system's clipboard features for security reasons, so if you are asking for a portable, pure-JavaScript answer, then I'm afraid you can't do it.

(If you must, you can search around the site for "javascript" and "clipboard", I think there are some approaches using Flash. But best not to do it at all.)

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • gmail can paste image from clipboard to editor without flash. – Dainius Jul 06 '11 at 14:53
  • @Dainius: How? In which browsers? – Kerrek SB Jul 06 '11 at 14:58
  • in linux works with chrome (just checked again), in windows i'm pretty sure works with news firefox, but don't have it her to check. How? Probably use some browser specific api, because I think there isn't any official html5 clipboard access yet. – Dainius Jul 08 '11 at 09:42
  • It'd be a security nightmare if there ever was! :-) I'm curious how it's implemented -- are you sure it's not using Flash? OK, let me take a look... [Edit:] How exactly do you paste something in GMail? I can't find it. – Kerrek SB Jul 08 '11 at 09:43
  • Downloaded windows vista image and tried with firefox5, it works also. Does not work with ie7 – Dainius Jul 08 '11 at 09:58
  • @Dainius: Can you walk me through the steps to paste something? I can't find it! :-( – Kerrek SB Jul 08 '11 at 10:10
  • Compose new gmail letter, take screen shot, enter mail text area and press Ctrl+V – Dainius Jul 08 '11 at 12:00
  • But this isn't a solution if you want to work with all browsers in all operating system. – Dainius Jul 08 '11 at 12:01
  • @Dainius: Hm, I can't make that work in Linux using Iron (=Chrome). Anyway, that's fine because that's a user interface action. What I was looking for is a pure JavaScript solution (e.g. clicking a button) that pastes the clipboard content. *That* would be scary. – Kerrek SB Jul 08 '11 at 12:03
1

For security reasons, JavaScript can't access the clipboard. If you really want to do it, you must use flash (flash can write into the clipboard, but not read). Yet, you need to click inside flash to write into clipboard (but it can be simulated).

You can see it being done in Pastebin (Copy to Clipboard).

If you really want to do it, I recommend you to use Zero Clipboard.

Notice that your actual approach is used by Google URL Shortener (try shorten a url).

Tell us your decision.

Ravan Scafi
  • 6,274
  • 1
  • 20
  • 32
  • Hey, thanks for your answer. Well, I guess the Zero Clipboard should work for me and I'm gonna use that one. Thanks again. – aborted Jun 27 '11 at 14:36
1

One of the APIs in the HTML5 family of APIs is about to solve your problem. It's called "Clipboard API and Events," and it will enable common clipboard activities (cut, copy, paste) for web apps via Javascript. Its currently (as of July 2011) a Working Draft. You can find the spec here:

http://www.w3.org/TR/clipboard-apis/

As for implementations, well, I couldn't find one just yet. Give it time...

james.garriss
  • 11,468
  • 6
  • 75
  • 94
0

or go to about:config and search this item on the list: "clipboard.autocopy = false" change it true! :D

tomfix
  • 1