Link to home
Start Free TrialLog in
Avatar of JR
JR

asked on

How to Copy to the Clipboard using Javascript?

Hello All,

How to Copy to the Clipboard using Javascript?

Could you suggest a solution that works for all the browsers.

Thanks,
Jothi
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Hi,

There isn't any script available to copy content to clipboard in JS for all browsers. However, using little help of flash player, you will be able to achieve this functionality.

Check- http://zeroclipboard.org
Avatar of JR
JR

ASKER

Is there a way without using flash?
Nope. Some scripts will work on IE, some on others. Only way I could find is this.
Anyways you do not need to show the flash component on form/page. It works behind the scene.
Avatar of JR

ASKER

Thanks. The flash extension should be enabled for the browser?
Just curious, that the user eventually wants to paste it to somewhere else right? otherwise you wouldn't need it to be in the clipboard.
So, if the user knows how to paste it to somewhere else, why doesn't he/she know how to copy it?
Yes, flash should be enabled. If it is not enabled; the js will fail.
what about select -> save to local storage
@Brian, @Scott - This seems very stupid when someone is implementing this kind of functionalities. But that depends on requirements where the selectable area for copy paste is not accessible to the user or disabled or not selectable, formatted text, etc.

I've been through similar critics!!

All apart, we know we can achieve this functionality by various ways (we're geeks after all). When it comes to normal user the story is different. And some apps needs to run on tabs/mobile devices. It will be difficult for users to copy the content to the clipboard.
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Scott,

I agree. Final decision would be of user for what he wants. And how the application functionality is required.
Avatar of JR

ASKER

Is it possible without flash usage to copy to clipboard other than IE?
Hi,

Copying has been disabled in most browsers as a security risk. However if you do not target IE, check this link for point #3 mentioned as workaround. It might or might not work. http://www.sitepoint.com/jquery-copy-clipboard-4-options/
@Rikin, yes you're right.
It's me being a consultant (who does everything except core development) for too long.  I always try to dig into the true reasons why our customers come out with those requirements.  Instead of taking the change request and sending them a quotation, sometimes I give them suggestions that solve their problems in a better way.

For example, if the requirement is to actually copy some data from page#1 and want to paste into page#2 (both in the same web app), Scott's solution (using sessionStorage/localStorage) is a better idea than using system clipboard.
Thank you.  

Please note, the article linked in http:/Q_28667156.html#a40759584 is old and "#3" using .live has long been depreciated.  Everything changes so fast  that I would even be suspect of an article 12 months old let alone 4years.
JR, I requested attention from a Moderator because I didn't see an actual answer from http:/Q_28667156.html#a40758040 and I also think it is misleading where the Expert mentions, "And some apps needs to run on tabs/mobile devices."  You have a better chance of using local storage than you do with flash which is not supported on iPhone and limited support on android devices.

The answer to, "how do I copy to the clipboard" is, "You can't do that" in the traditional manner for security reasons. However, you can give the same experience and using local storage is one such way.  Here is an opensource github project that does this too http://zeroclipboard.org/

Something that is a bit confusing on this site is the "Accept" links are above the solution and not below.  I wonder if you meant to actually select comment #40758157
Hi Scott,

The opensource github solution zeroclipboard you have suggested also uses flash component, that author does not want to use.
Yes, and that's why the my suggestion of using local storage is the way to go and except for opera mini, works on every other browser including ios and android http://caniuse.com/#search=localstorage