I struggled for hours to find out how to make a javascript function to copy the current URL into a new alert window. For example, when a user click "Share this page", then a new alert window appears with the URL selected in an input text box:
How can I make it done with javascript?
Thanks.
I struggled for hours to find out how to make a javascript function to copy the current URL into a new alert window. For example, when a user click "Share this page", then a new alert window appears with the URL selected in an input text box:
How can I make it done with javascript?
Thanks.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 14, 2015 at 10:31 PeterPeter 7324 gold badges10 silver badges25 bronze badges4 Answers
Reset to default 9As simple as this:
prompt('Please copy the following URL:', window.location);
prompt('Please copy the following URL', window.location.href)
For a better user experience you may consider allowing access to the clipboard with clipboard.js.
You can use the simple snippet code, window.location
as the second param in prompt
.
window.copyURL = function(){
prompt("Press [Ctrl + c] to copy the url :",window.location);
}
<button onclick="copyURL()">Copy URL </button>