You need to sign in to do that
Don't have an account?
M Relova 9
Copy to clipboard only works on 2nd to nth click
Does anyone know why our Javascript code works only on the 2nd to nth try in SFDC, but on 1st attempt in stand-alone HTML?
We are creating "button" that executes Javascript to copy certain contents of a Case into the clipboard. The same result is seen under Chrome, IE, Safari, and Firefox.
The problem code we're attaching to the button is below. Works 2nd to nth try:
And here is the same code in a standalone HTML which works 1st to nth click:
We are creating "button" that executes Javascript to copy certain contents of a Case into the clipboard. The same result is seen under Chrome, IE, Safari, and Firefox.
The problem code we're attaching to the button is below. Works 2nd to nth try:
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")} console.log('browser supports copy?: ' + document.queryCommandSupported("copy")); var text = 'Ticket #: {!Case.Id}\n'; //window.prompt("Copy to clipboard: Ctrl+C, Enter", text); var tempInput = document.createElement("textarea"); tempInput.setAttribute('id', 'copyid'); tempInput.value = text; document.body.appendChild(tempInput); i = document.getElementById('copyid'); i.select(); try { var result = document.execCommand("Copy"); // clicking button this JS is embedded in first time results in a FALSE // clicking button this JS is embedded in second to nth time results in a TRUE console.log('execcommand copy result: ' + result); } catch (e) { console.log('Copy exception: ' + e); }
And here is the same code in a standalone HTML which works 1st to nth click:
<script> function doit() { console.log ('browser supports copy?: ' + document.queryCommandSupported("copy")); var text = 'blah blah'; var tempInput = document.createElement("textarea"); tempInput.setAttribute('id', 'copyid'); tempInput.value = text; document.body.appendChild(tempInput); document.getElementById('copyid').select(); try { var result = document.execCommand("copy", false, null); // clicking button this JS is embedded in first time results in a FALSE // clicking button this JS is embedded in second to nth time results in a TRUE console.log('execcommand copy result: ' + result); } catch (e) { console.log('Copy exception: ' + e); } document.body.removeChild(tempInput); </script> <input type="button" value="copy text" onClick="doit()">
Did you tried it on all browser? If it working on any of the major browser, then definitely one element must be browser specific.
Or try to use static text in "var text" variable (no Case.Id)
Thanks,
Satya