最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How would I implement 'copy url to clipboard' from a link or button using javascript or dojo without flash - Sta

programmeradmin2浏览0评论

I got stucked in implementing this feature on my web application. All the other possibilities are mostly by using flash content. Could someone explain how I can achieve it by using plain javascript or by Dojo.

I got stucked in implementing this feature on my web application. All the other possibilities are mostly by using flash content. Could someone explain how I can achieve it by using plain javascript or by Dojo.

Share Improve this question edited Sep 11, 2015 at 13:58 Cédric Boivin 11.4k13 gold badges59 silver badges101 bronze badges asked May 13, 2013 at 16:24 RaghuramRaghuram 891 gold badge2 silver badges9 bronze badges 5
  • Which browsers would you like to support? – demee Commented May 13, 2013 at 16:30
  • 2 Flash is commonly used because you cannot use JavaScript to reliably copy to the users clipboard; stackoverflow.com/questions/14460210/… – Alex K. Commented May 13, 2013 at 16:31
  • @SlawomirDemichowicz I am looking for cross browser solution. I come to know that there is a solution in IE – Raghuram Commented May 13, 2013 at 16:44
  • Possible duplicate of stackoverflow.com/questions/127040/…? – sixFingers Commented May 13, 2013 at 17:35
  • @Raghuram there is now a better way. See my answer. – Willem Mulder Commented Sep 8, 2020 at 10:54
Add a comment  | 

5 Answers 5

Reset to default 5

I have been working on the exact same issue for a while. For me flash isn't a viable solution so I came up with this simple work around:

<button onclick="prompt('Press Ctrl + C, then Enter to copy to clipboard','copy me')">Click to Copy</button>

It requires some extra work on the users end but at least it doesn't require flash or external libraries.

example fiddle

Wanted to implement the same feature. Ended up using https://clipboardjs.com.

new Clipboard('.btn', {
  text: function() {
    return window.location.href;
  }
});

Works well

Html

<a class="" data-toggle="tooltip" data-placement="top" title="Copy profile Link" onclick="copy_to_clipboard('<%=public_profile_url(user.public_id)%>')">
<i class="fa fa-copy"></i>

Css

#url_public_id{
  display: none;
}

JS

function copy_to_clipboard(link) {
    $("#url_public_id").show()
    var Url = document.getElementById("url_public_id");
    Url.select();
    document.execCommand("copy");
    $("#url_public_id").hide()
    alert("Copied URL ");
}

It has been a long time, but this now works:

document.execCommand('copy');

Which copies the currently selected text to the clipboard. If you want to copy a specific text using javascript, you would have to create a fake input element in the DOM, then do the following

let element = document.getElementById('yourID');
element.value = 'yourText';
element.select();
element.setSelectionRange(0, element.value.length);
document.execCommand('copy');

check my code it's working

static getFileUrl(id){
    var url=new URL("http://localhost:3000/FileUrl")
    url.searchParams.append('id',id)
    return url.href
}
static copyFileUrl(id){
    copy(this.getFileUrl(id))
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论