Michael Mahony Michael Mahony - 10 months ago 116
Javascript Question

Copy URL from browser using clipboard.js

I am using the Accusoft document viewer. I have a requirement for a button that when clicked will copy to the clipboard the current URL plus a page number querystring parameter. I am using clipboard.js. I know exactly how to get the current URL and how to add a page number parameter to the URL. What I don't know how to do is make clipboard.js copy a variable (such as the generated URL with page number parameter) to the clipboard. Any help on this?


Clipboard.js creator here ;)

You can use the imperative API to achieve that:

var url = document.location.href;

new Clipboard('.btn', {
  text: function() {
    return url;
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.10/clipboard.min.js"></script>

<button class="btn">Copy</button>