Fmdpa Posted June 10, 2010 Share Posted June 10, 2010 There are dozens of scripts out there to copy selected text, or even to copy text in a textbox, but I want to copy some predefined text. <html><head><script type="text/javascript">function copy(){??? //What would the script be to copy a phrase, e.g.: "Hello World"?}</script></head><body><input type="button" value="Copy" onclick="copy()"></body></html> Link to comment Share on other sites More sharing options...
jeffman Posted June 10, 2010 Share Posted June 10, 2010 var x = 'Whatever you want on the clipboard';window.clipboardData.setData('Text',x); That still works in IE7. I don't know about IE8. A warning pops up in 7 giving the user a chance to decline, so if you're trying to do something sneaky, it probably won't work.This and the other code you mentioned do not function in any other browsers besides IE. Something to consider. Link to comment Share on other sites More sharing options...
Fmdpa Posted June 10, 2010 Author Share Posted June 10, 2010 IE 8 gave an Allow/Don't Allow confirmation message, but Firefox didn't allow it, period. It isn't for something "sneaky", but if the user has to go through the confirmation message, then it sort of defeats the purpose of having the copy button, which would be for convenience. Link to comment Share on other sites More sharing options...
Fmdpa Posted June 10, 2010 Author Share Posted June 10, 2010 Are there any cross browser compatible codes? Link to comment Share on other sites More sharing options...
jeffman Posted June 10, 2010 Share Posted June 10, 2010 No. I can certainly imagine ways that such a tool could be useful and convenient. The problem is that an evil developer could also exploit it with malicious intent. All the major browser makers besides IE have decided not to allow it at all, and the tendency these days is to increase security, not to decrease it. So don't expect Firefox and the others to change. If anything, IE will eventually drop it. Link to comment Share on other sites More sharing options...
jeffman Posted June 10, 2010 Share Posted June 10, 2010 FWIW:I just remembered that a "superuser" CAN specially configure Firefox to allow this behavior. But you can't do it for the user. Each user has to change the default settings individually. And for most users, the technique looks pretty scary. It's not one of those convenient preferences panels. It's more of a line-item config file.If you were configuring a custom tool for a corporation, this solution could work. For a regular page you expect anyone to access, this is really not a solution. Link to comment Share on other sites More sharing options...
End User Posted June 10, 2010 Share Posted June 10, 2010 Are there any cross browser compatible codes?Well...there's a way to do it, but it's rather clumsy. You have a button that selects the div, textarea, table cell, etc via Javascript, and then prompt the user to use CONTROL-C to copy the text. It's lame, but it works in almost any browser. I'm not a big fan of "direct-to-clipboard" operations in the browser. As Deirdre's Dad pointed out, it's a great way for a malicious developer to use it against you in an almost unlimited number of ways. Link to comment Share on other sites More sharing options...
Fmdpa Posted June 10, 2010 Author Share Posted June 10, 2010 Thanks for the responses. I think I've decided by now to drop the idea. Link to comment Share on other sites More sharing options...
jeffman Posted June 11, 2010 Share Posted June 11, 2010 One of the things I like about this board is that the regulars generally care about things like accessibility and other ethical issues that are easily forgotten. I think being one of the "good guys" is always the right choice, even if that just means (as in this case) choosing not to use a technology that other developers might exploit -- that is to say, leading by example. Link to comment Share on other sites More sharing options...
factor5 Posted June 18, 2010 Share Posted June 18, 2010 Are there any cross browser compatible codes?Sure there are solutions based on js and flash/as link. I've been using one like this in my project. Link to comment Share on other sites More sharing options...
Guest Rod Posted July 14, 2010 Share Posted July 14, 2010 >>The problem is that an evil developer could also exploit it with malicious intent.When some of the browsers allowed JavaScript copy to clipboard without user intervention there was the possibility of malicious behavior.When the browser user must take action (click a button) to copy to the clipboard, there is relatively little is any risk to the user.That's the case in the cross-browser method I demonstrate at http://www.rodsdot.com/javascript/cross_br...ver_message.asp which is linked in the message above. Link to comment Share on other sites More sharing options...
RobberBaron Posted July 21, 2010 Share Posted July 21, 2010 One thing you could consider is having a web clipboard where the data is saved to the site, rather than the computer. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.