For my programming culture I'm experimenting with audio html tag and audio sources, and OS clipboard. It's 3 days I'm trying to solve the issue exposed as follows.
Situation
I have an html page on server with a js which has a bunch of code in it. The js does a lot of things, among the others generates two inputs inserting where I need the appropriate 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.
The Behavior
- If I click the button the related onclick function copyTitleOnClipboard() executes and copies the value of the input into the OS clipboard.
- If I write by hands the name of the function copyTitleOnClipboard() into the Google Chrome console it works too.
For me this means that the copyTitleOnClipboard() function per se works properly. 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
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?
- How do I solve the issue?
Thanks in advance.