What I am doing is programmatically select all text from a webpage and then copy it. The select all works with execCommand
but copy doesn't.
Here is my code:
$.ajax({
url: $('#url').val(),
type: 'GET',
success: function(res) {
$('#result').html(res.responseText);
$('#result').fadeIn('fast');
$('#result').focus();
$('#result').select();
document.execCommand('selectall');
// copy does not work ?
document.execCommand('copy');
}
});
Here is Example on JsBin
I also tried using flash solution such as ZeroClipboard, however it seems that one has to press their flash object/button explicitly to copy text whereas I wanted to do zeroclip.setText('whatever');
without user's pressing the button.
Can anyone tell how to copy text programmatically?
What I am doing is programmatically select all text from a webpage and then copy it. The select all works with execCommand
but copy doesn't.
Here is my code:
$.ajax({
url: $('#url').val(),
type: 'GET',
success: function(res) {
$('#result').html(res.responseText);
$('#result').fadeIn('fast');
$('#result').focus();
$('#result').select();
document.execCommand('selectall');
// copy does not work ?
document.execCommand('copy');
}
});
Here is Example on JsBin
I also tried using flash solution such as ZeroClipboard, however it seems that one has to press their flash object/button explicitly to copy text whereas I wanted to do zeroclip.setText('whatever');
without user's pressing the button.
Can anyone tell how to copy text programmatically?
Share Improve this question edited Jan 10, 2013 at 9:59 Dev01 asked Jan 10, 2013 at 8:32 Dev01Dev01 4,2225 gold badges32 silver badges46 bronze badges 1- Perhaps, you could do something which I mentioned here: SOLVED: document execCommand copy not working with AJAX – Nishanth Matha Commented Apr 13, 2017 at 6:00
1 Answer
Reset to default 5The copy mand used to be protected in all browsers but IE (it would not work in other browsers). Requesting the user use Ctrl+C was a mon workaround.
As of Firefox 41 (September 2015), Chrome 42 (April 2015) and Opera 29 (April 2015) this is no longer the case the copy mand should be available by default in most major browsers when triggered from certain trusted (user-triggered) events, such as what would be fired in response to a button click.
The patibility table from MDN, and W3C bug offer further information.