38

I write the path of a document into the alert box via using below code.

var oArg = new Object();
oArg.Document = $(t).attr("path") + str + "/" + $(t).attr("name");
alert(oArg.Document);

Assume that message is : "documents/files/img/stack.jpg"

I only want to copy this text with a button. For Chrome Ctrl + C is ok for it but for IE, Ctrl + C copies everything at the alertbox.

How can I copy only the message with using a button?

Ctrl + C works like below:

  • Chrome - Works perfectly.
  • Internet Explorer - Works, but you get extra text. The caption and OK button text is also copied, along with a bunch of dashes. This is almost never what you want.
  • Firefox - Doesn't work at all You must select the text before you can copy it.
4

3 回答 3

82

What you can do is to prompt the user with the text and ask them to copy it. As such:

prompt("Copy to clipboard: Ctrl+C, Enter", oArg.Document);

Because if you supply a text to the prompt it automatically gets selected. Does this suit you?

于 2013-07-11T11:02:24.147 回答
20

When ever the alert message box appears with the text message , just press CTRL+ C and your message will be copied, then you can paste it anywhere you want , no need to write it or using any other method to get the text.

于 2015-07-10T10:08:41.857 回答
12

I've found that, in Chrome at least, the alert box text is in fact selectable -- when the text does not contain any line-breaks (ie. "\n").

I don't know why this is the case, but I've had to use this trick before for quick selection prompts. (you could also use prompt("please press ctrl+c to copy the text below", "text to copy"), but that usually looks worse)

于 2017-06-27T06:14:15.673 回答