Jump to content

Copying text to the clipboard


Fmdpa

Recommended Posts

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

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

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

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

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

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

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

  • 4 weeks later...

>>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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...