简体   繁体   English

使用javascript复制选定的输入框无法正常工作

[英]Copy selected input box with javascript doesn't work properly

For my programming culture I'm experimenting with audio html tag and audio sources, and OS clipboard. 对于我的编程文化,我正在尝试使用音频html标签和音频源以及操作系统剪贴板。 It's 3 days I'm trying to solve the issue exposed as follows. 这是我试图解决暴露问题的3天,如下所示。

Situation 情况

I have an html page on server with a js which has a bunch of code in it. 我在服务器上有一个带有js的html页面,里面有一堆代码。 The js does a lot of things, among the others generates two inputs inserting where I need the appropriate html: js做了很多事情,其他人生成两个输入插入我需要适当的html:

a text input: 文字输入:

<input type="text" id="titleToCopy" value="" />

and a button:

    <input type="button" value="Copy" onclick="copyTitleOnClipboard()"/>


Into the js code there are also these two functions

function execThings() //This execute some operations and also calls other functions
{
    console.log(arguments.callee.name);
    var dest = document.getElementById(myplayerID); // Gets my player by its ID
    var osrc = getOriginalPlayer().src;
    dest.src = osrc;
    updateTitleToCopy();
    copyTitleOnClipboard();
    stopOriginalPlayer();
}


function copyTitleOnClipboard() // This function select and copies to the Operative System clipboard the content of the related text input
{
    console.log(arguments.callee.name);
    var titleInput = document.getElementById("titleToCopy");
    titleInput.focus(); // The function works good with or without this line
    titleInput.select();

    var r = document.execCommand("copy");
    r = r === true ? "has been" : "not";

    console.log("Title " + r + " copied to clipboard");
}

For debugging purposes I added in both functions the row 出于调试目的,我在两个函数中添加了行

console.log(arguments.callee.name);

In this way on Chrome console is shown the name of the function, so I can check if it starts execution. 这样在Chrome控制台上会显示该功能的名称,因此我可以检查它是否开始执行。

The Behavior 行为

  • If I click the button the related onclick function copyTitleOnClipboard() executes and copies the value of the input into the OS clipboard. 如果单击该按钮,相关的onclick函数copyTitleOnClipboard()将执行并将输入值复制到OS剪贴板中。
  • If I write by hands the name of the function copyTitleOnClipboard() into the Google Chrome console it works too. 如果我手动将函数copyTitleOnClipboard()的名称写入谷歌Chrome控制台,它也可以。

For me this means that the copyTitleOnClipboard() function per se works properly. 对我来说,这意味着copyTitleOnClipboard()函数本身可以正常工作。 In fact on the console in both cases I get the debug feedback thanks to the row: 实际上在两种情况下都在控制台上我得到了调试反馈,感谢行:

console.log("Title " + r + " copied to clipboard");

which as expected returns the output Title has been copied to clipboard 按预期返回输出标题已复制到剪贴板

And if I paste elsewhere (for example into notepad) the content of the clipboard the result is the value of the text box input, as expected. 如果我粘贴到其他地方(例如记事本)剪贴板的内容,结果是文本框输入的值,如预期的那样。

The problem 问题

The problem is that when the execution is due to the other function execThings() , which calls the copyTitleOnClipboard() function, it doesn't work anymore: the copyTitleOnClipboard() function is executed in fact on the console is shown its name as expected but I also get the fail feedback message: Title not copied to clipboard and if I paste the clipboard content in notepad it doesn't contain the text box value that has to be copied or is empty at all 问题是,当执行是由于其他函数execThings()调用copyTitleOnClipboard()函数时,它不再起作用: copyTitleOnClipboard()函数实际上在控制台上执行,其名称显示为预期但我也得到了失败的反馈信息: 标题没有复制到剪贴板 ,如果我将剪贴板内容粘贴到记事本中,它不包含必须复制的文本框值或者根本就是空的

Questions 问题

  • Why calling the copyTitleOnClipboard() function from another function doesnt' work as if I call it by clicking the button or by hands by the Chrome console? 为什么从另一个函数调用copyTitleOnClipboard()函数并不像我通过点击按钮或Chrome控制台手动调用它一样?
  • How do I solve the issue? 我该如何解决这个问题?

Thanks in advance. 提前致谢。

eeeehhhmmm... eeeehhhmmm ...

Sir I've copied your code and replaced copyTitleOnClipboard() with execThings() in the onclick="copyTitleOnClipboard()" of the button.... And it works properly, execThings() and copyTitleOnClipboard() are printed in the console and the value of the input box is copied to the clipboard and I verified that... 先生,我复制你的代码,代之以copyTitleOnClipboard()execThings()onclick="copyTitleOnClipboard()"按钮的....而且它正常工作, execThings()copyTitleOnClipboard()打印在控制台和输入框的值被复制到剪贴板,我验证了...

this clearly states that the problem is with the other code omitted from the question and replaced with the //some other code comment. 这清楚地表明问题是从问题中省略了其他代码并用//some other code注释替换。 Please include that code in addition to the code of the question because I think something there messes up the execThings() function. 除了问题的代码之外,请包含该代码,因为我认为有些东西execThings()execThings()函数。

I was able to replicate the issue you are facing. 我能够复制你所面临的问题。

Checkout this code here: https://jsfiddle.net/t1pe6zw8/ 在这里查看此代码: https//jsfiddle.net/t1pe6zw8/

Open this link, wait for 5 seconds and see what happens. 打开此链接,等待5秒钟,看看会发生什么。

Here is the explanation for the behavior: 以下是该行为的解释:

I am assuming here that the execThings() function is triggered by some event that's not raised by user action. 我在这里假设execThings()函数是由一些不是由用户操作引发的事件触发的。

The basic JS method document.execCommand(), requires that it be triggered by user interaction to prevent web pages from messing with the clipboard without the user's knowledge. 基本的JS方法document.execCommand()要求它由用户交互触发,以防止网页在用户不知情的情况下弄乱剪贴板。 This is a security feature and is enforced by every browser. 这是一项安全功能,由每个浏览器强制执行。

$(document).ready(function(){
    setTimeout(function(){ execThings(); }, 5000);
});

This code above, once the document is ready, triggers the execThings() function after 5 seconds and since it's triggered without user action, it fails to copy the text. 上面的代码,一旦文档准备就绪,在5秒后触发execThings()函数,并且由于它是在没有用户操作的情况下触发的,因此无法复制文本。

Now, if you click Click Me to Exec Things button on JSFiddle link above, it triggers the same execThings() function, and it is able to copy the text. 现在,如果您在上面的JSFiddle链接上单击Click Me to Exec Things按钮,它会触发相同的execThings()函数,并且它能够复制文本。

You can get more details here: https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Return_value 您可以在此处获取更多详细信息: https//developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Return_value

Now, I am not sure what are your use-cases, but you will need to get user action to copy the text. 现在,我不确定您的用例是什么,但您需要让用户操作来复制文本。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM