Copy/Paste in JavaScript?
I know this question was asked like a million times by now, but I couldn't really fin开发者_运维问答d a good up-to-date solution.
I've implemented my own menu to provide the user the ability to Cut, Copy and Paste into my WebApp. But I'm not sure how to actually work with the clipboard on Firefox, IE, Safari/Chrome.
Thank you for your help.
I just wrote a detailed technical blog post on this very subject (I work for Lucidchart and we recently did an overhaul on our clipboard). Included in the post is this fiddle which is a working example of copying and pasting via Javascript.
The good news is that this example gives you working code for setting/getting any supported clipboard data types whenever the user uses a clipboard hotkey.
The bad news is that using your own context menu to copy and paste is problematic. Even Google can't get around this (try using context-menu copy or paste in Google Docs in Firefox). You'll be able to get it to work without too much trouble in IE. This is because you can access the clipboardData object at anytime from Javascript via:
window.clipboardData
(When you attempt to do this outside of a system cut, copy, or paste event, however, IE will prompt the user to grant the web application clipboard permission.)
In Chrome, you can create a chrome extension that will give your web app clipboard permissions (this is what we do for Lucidchart). Then for users with your extension installed you'll just need to fire the system event yourself when they click the menu option:
document.execCommand('copy');
It looks like Firefox has some options that allow users to grant permissions to certain sites to access the clipboard, but I haven't tried any of these personally.
did u try : http://ericphan.info/development/cross-browser-copy-and-paste-with-jquery-copy/
UPDATE: the link is not available so i copy the content from cache :
The Scenario
I was working on a client project for SSW when the client reported a bug in the web app.
The bug involved a dynamically generated mailto link that got updated when you selected multiple employees. The client was reporting an error when he selected more than 10 employees to email. His Lotus Notes mail client popped up an error saying:
Error processing command line arguments
Testing this myself I found that Outlook 2007 could easily support the emails of 30-40 employees before the mailto link stopped working. The Cause
It turns out that the mailto spec has a limit and the mail clients also have a limit. Lotus Notes only handles 240 characters in the mailto link and other modern mail clients like Outlook 2007 support the 2083 characters - the max length of a URL
This explains the discrepancy in testing. The fix - JQuery to the rescue
Since this is a limitation of the HTML spec we needed another solution to meet the client’s requirement of “I want to be able to select multiple employees and send an email to all of them”
We could have created an email form that used SMTP to send out the email - but the client wanted to use Lotus Notes as his mail client.
We ended up changing the “email” button to copy all the emails (comma separated) onto the clipboard and popped open a new email window. All the client had to do was hit CTRL + V and paste the emails into the TO field. This was the quickest and most cost effective solution that gave the client the flexibility to use their own email client.
There is a JQuery plugin called jquery.copy that provided cross browser copy and paste by using a flash (swf) file. This is similar to how the syntax highlighter on my blog works.
Once you reference the jquery.copy.js file all you need to do to push data into the clipboard is run the following:
$.copy("some text to copy");
Nice and easy ;)
Note: you may need to change the path the the SWF file in jquery.copy.js to get this to work
精彩评论