javascript - firefox中document.execCommand('copy')无效

查看:144
本文介绍了javascript - firefox中document.execCommand('copy')无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

前端菜鸟在查一个firefox复制失败但chrome复制正常的问题。定位到firefox执行document.execCommand('copy')返回false,而chrome返回true

这里提到

With the 'cut' or 'copy' command as argument, Document.execCommand()
now works, but only within the context of user-initiated or privileged
code

我使用的是firefox 47

看了代码感觉execCommand('copy')不是用户触发的(我不确定,不了解多少js....)
想问下privileged code是个什么东西……firefox中有开关能打开之类的么?

谢谢。

解决方案

这是一个安全考虑,因为exeCommand()可以操作系统剪切板,有可能被恶意利用。所以你不能用JS直接调用execCommand('copy'),而需要放到某一个有用户出发的事件响应函数内,如

<button id="mybtn"></button>
<script>
  $('#mybtn').click(function () {
    document.execCommand('copy');
  });
</script>

这篇关于javascript - firefox中document.execCommand(&#039;copy&#039;)无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆