最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript - How can I copy a text to clipboard without displaying a textarea? - Stack Overflow

programmeradmin2浏览0评论

I'm trying to copy to the user's clipboard a path to the log files in Javascript. However, I do not wish to use an input field or textarea. Other solutions indicate that I should use an input field, select the text from the field, then copy it. Is there any workaround to have a code-defined string directly copied to my clipboard? Thanks.

  • I use plain JS and Node.js
  • I am trying to copy a text defined in my code (local path to log files) to the user's clipboard without using any user input fields (textarea, input tags). The other questions seem to either use user fields or 3rd party APIs. I do not want that! I am looking for some JS code that will acplish all this.

I'm trying to copy to the user's clipboard a path to the log files in Javascript. However, I do not wish to use an input field or textarea. Other solutions indicate that I should use an input field, select the text from the field, then copy it. Is there any workaround to have a code-defined string directly copied to my clipboard? Thanks.

  • I use plain JS and Node.js
  • I am trying to copy a text defined in my code (local path to log files) to the user's clipboard without using any user input fields (textarea, input tags). The other questions seem to either use user fields or 3rd party APIs. I do not want that! I am looking for some JS code that will acplish all this.
Share Improve this question edited Nov 12, 2020 at 12:01 Andhi Irawan 4808 silver badges17 bronze badges asked Jun 30, 2017 at 15:25 gatsbyzgatsbyz 1,0751 gold badge11 silver badges26 bronze badges 12
  • 1 I assume that you want to do this in a browser? – Davin Tryon Commented Jun 30, 2017 at 15:26
  • @DavinTryon Ah, yes! I forgot to mention that I am building a web application, however, produced in a batch file. I am testing and running in web, but the production will ultimately be in a batch file. – gatsbyz Commented Jun 30, 2017 at 15:28
  • Possible duplicate of How do I copy to the clipboard in JavaScript? – evolutionxbox Commented Jun 30, 2017 at 15:29
  • 1 So you want to copy to the clipboard from node? – Davin Tryon Commented Jun 30, 2017 at 15:29
  • 1 @PraveenKumar Haha. No problem :) I actually liked your edit better. – gatsbyz Commented Jun 30, 2017 at 19:09
 |  Show 7 more ments

1 Answer 1

Reset to default 7

As you have already read from other post, you need the textarea or textbox to copy to clipboard, because of browser security, If you don't want to show the textbox or textarea, just hide it, you can use

.hide {
  position:absolute;
  left:-9999px
}

left: value should be greater either positive or negative, i.e it does not fall into any current screen visible width.

发布评论

评论列表(0)

  1. 暂无评论