javascript - Using the Clipboard API - Throws error "is not defined" - Stack Overflow

So as has been encounteredasked before I'm trying to make a copy function that works on non-flash

So as has been encountered/asked before I'm trying to make a copy function that works on non-flash devices.

This has led me to the Clipboard API.
But I can't seem to get it working properly, this is what I have so far:

$(function () {
    $(document).on( 'click', '.copy-btn', function (e) {
        var data = 'This has been copied';

        var clip = new ClipboardEvent( 'copy' );
        clip.clipboardData.setData( 'text/plain', data );
        clip.preventDefault();

        e.target.dispatchEvent( clip );
    });
});

The code is quite self-explanatory, on a click of the button it should copy "This has been copied" to the clipboard. Upon trying to do so I get:
"Uncaught ReferenceError: ClipboardEvent is not defined". What am I missing? Shouldn't this be "built in" or is there a need to call the API in anyway?
Also stumbled upon this which gives the same error.

This seems to be a simple misstake, or is the API currently not working?

So as has been encountered/asked before I'm trying to make a copy function that works on non-flash devices.

This has led me to the Clipboard API.
But I can't seem to get it working properly, this is what I have so far:

$(function () {
    $(document).on( 'click', '.copy-btn', function (e) {
        var data = 'This has been copied';

        var clip = new ClipboardEvent( 'copy' );
        clip.clipboardData.setData( 'text/plain', data );
        clip.preventDefault();

        e.target.dispatchEvent( clip );
    });
});

The code is quite self-explanatory, on a click of the button it should copy "This has been copied" to the clipboard. Upon trying to do so I get:
"Uncaught ReferenceError: ClipboardEvent is not defined". What am I missing? Shouldn't this be "built in" or is there a need to call the API in anyway?
Also stumbled upon this which gives the same error.

This seems to be a simple misstake, or is the API currently not working?

Share Improve this question edited May 23, 2017 at 11:50 CommunityBot 11 silver badge asked Apr 23, 2014 at 7:56 SeloSelo 2501 gold badge3 silver badges10 bronze badges 3
  • With which browser you're trying to do it? – Adriano Repetti Commented Apr 23, 2014 at 7:59
  • I've tried with IE 10 and Chrome v34, but end goal is getting it to work on mobile browsers – Selo Commented Apr 23, 2014 at 8:03
  • @user3088863: Neither of those supports the ClipboardEvent constructor. – T.J. Crowder Commented Apr 23, 2014 at 8:03
Add a ment  | 

1 Answer 1

Reset to default 2

To date, only Firefox supports the ClipboardEvent constructor; support info.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745330724a4622865.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信