我有一个复制文本按钮,我正在使用ZeroClipboard来复制页面上的某些文本。它适用于Chrome和IE,但它不会在Firefox中复制文本,complete
事件永远不会被触发。
我设置按钮的JavaScript看起来像这样:
ZeroClipboard.setDefaults({
moviePath: '/js/zeroclipboard/ZeroClipboard.swf',
allowScriptAccess: 'always',
forceHandCursor: true
});
function enableCopyButton(container) {
var button = container.find('.text-copy'),
source = container.find('.text'),
clip = new ZeroClipboard(button);
clip.setText(source.val());
clip.on('load', function (client) {
console.log('ZeroClipboard loaded.');
client.on('complete', function (client, args) {
console.log('Text copied: ' + args.text);
});
});
clip.on('noFlash', function () {
console.error('No Flash installed!');
});
clip.on('wrongFlash', function () {
console.error('Wrong Flash installed!');
});
}
控制台最终显示"ZeroClipboard loaded."
而没有其他内容。不会抛出任何错误,我已确认正在加载ZeroClipboard.swf
并将其放置在页面上。 mousedown
和mouseup
事件也被解雇了。正在运行的页面使用的是有效的SSL证书,页面上的所有资源都是通过HTTPS加载的。
GitHub上的库的演示页面在FireFox中运行良好,所以我怀疑它是我正在做的事情。
答案 0 :(得分:1)
我不确定这是否会有所帮助,但最近我一直在使用zeroclipboard超过一个月。有时候它会起作用,但有时会由于一些非常微小的事情而失败(也许是因为我对javascript和html的东西很新)...而且我很清楚这种不安...
在你的情况下,你以前曾经尝试过这个活动吗? 而是单独使用clip.setText(source.val()),将其移到'dataRequested'事件中,如下所示:
clip.on('dataRequested', function(client, args) {
client.setText(source.val());
});
然后在单击按钮后,查看是否触发了完整事件。
顺便说一句,我想知道你的'noflash'或'wrongflash'是否正常被解雇?在我的情况下,如果用户没有安装闪存,事件仍然没有被触发......不确定它有什么问题......无论如何,祝你好运:)
答案 1 :(得分:0)
我的开发环境:
以下是我使用Copy to Clipboard在5个浏览器中工作的方法:
我的情景: 我要复制到剪贴板的文本生成一个Div和html break(br)。 对于Copy,我需要删除这些html中断并用/ r / n替换它们。 该副本是通过按钮单击。
不是这可能不是编写代码的最好方法,但它对我有用。
获取最新的ZeroClipboard在我的.cshtml文件中,我定义了按钮和div并包含ZeroClipboard.min.js文件。
<!-- language-all: lang-html -->
<button id="btCopyToClipboard" name="btCopyToClipboard" type="button">Copy To Clipboard</button>
<div id="Basket" ></div>
Javascript部分:
<!-- language: lang-js -->
<script type="text/javascript">
$(document).ready(function () {
//"style" the buttons
$("#btCopyToClipboard").button();
//ZeroClipboard code
var clip = new ZeroClipboard(document.getElementById('btCopyToClipboard'), {
moviePath: "/Scripts/ZeroClipboard.swf", // URL to movie
trustedOrigins: null, //null Page origins that the SWF should trust (single string or array of strings)
hoverClass: "", // The class used to hover over the object
activeClass: "", // The class used to set object active
allowScriptAccess: "always", //sameDomain SWF outbound scripting policy
useNoCache: true, // Include a nocache query parameter on requests for the SWF
forceHandCursor: true //false Forcibly set the hand cursor ("pointer") for all glued elements
});
//if just using var clip = new ZeroClipboard(); then need to use .glue(..)
//clip.glue(document.getElementById('btCopyToClipboard'));
clip.on('load', function (client, args) {
DebugLog("ZeroClipboard.swf is loaded and user's flash version is: " + args.flashVersion);
});
//The complete event is fired when the text is successfully copied to the clipboard.
clip.on('complete', function (client, args) {
//alert("clip.onComplete(..) -- Copied text to clipboard args.text: " + args.text);
});
clip.on('mouseover', function (client) {
// alert("mouse over");
});
clip.on('mouseout', function (client) {
//alert("mouse out");
});
clip.on('mousedown', function (client) {
//alert("mouse down");
});
clip.on('mouseup', function (client) {
//alert("mouse up");
});
clip.on('dataRequested', function (client, args) {
//get text from basket
var txt = $("#Basket").html();
//to make Notepad honour line breaks, we have to do some magic
var windowsText = txt.replace(/\n/g, '\r\n');
//replace html break with line breaks
windowsText = windowsText.replace(/<br\s*\/?>/g, "\r\n");
client.setText(windowsText);
});
clip.on('noflash', function (client, args) {
var msg = "You don't support flash, therefore the Copy To Clipboard will not work.";
DebugLog(msg);
alert(msg);
});
clip.on('wrongflash', function (client, args) {
var msg = 'Your flash is too old ' + args.flashVersion + '. The Copy To Clipboard supports version 10 and up.';
DebugLog(msg);
alert(msg);
});
function DebugLog(message) {
if (console && console.log) {
console.log(message);
}
}
});//eof $(document).ready
</script>
答案 2 :(得分:0)
最新版本的zeroclipboard使用了在版本28.0之前的firefox中不存在的event.stopImmediatePropagation,它失败并显示错误:
event.stopImmediatePropagation is not a function
您可以在此处查看浏览器比较:
http://compatibility.shwups-cms.ch/de/home?&property=TrackEvent.prototype.stopImmediatePropagation
我使用此polifil添加缺少的功能: