我有一个复制文本按钮,我正在使用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中运行良好,所以我怀疑这是我正在做的事情。
我不确定这是否有帮助,但最近我已经使用零剪贴板一个多月了。有时它有效,但有时由于一些非常微小的事情而失败(可能是因为我对javascript和html的东西很陌生)。。。我很理解这种沮丧。。。
就你而言,你以前尝试过这个活动吗?相反,单独使用clip.setText(source.val()),将其移动到"dataRequested"事件中,如下所示:
clip.on('dataRequested', function(client, args) {
client.setText(source.val());
});
然后单击按钮后,查看是否触发了整个事件。
顺便说一句,我想知道你的"noflash"或"错误flash"是否正常被解雇了?在我的情况下,如果用户没有安装闪存,事件仍然不会启动。。。不确定它出了什么问题…
无论如何,祝你好运:)
我的开发环境:
- .NET 4.5
- 带有Razor引擎的ASP.NET MVC4
- jQuery
以下是我所做的让"复制到剪贴板"在5个浏览器上工作的方法:
- FF 23
- IE 10
- 铬29
- Safari 5.1.7
- Opera 16
我的场景:我想复制到剪贴板的文本是在Div中生成的,并带有html换行符(br)。对于Copy,我需要删除这些html中断,并将其替换为/r/n。通过点击按钮进行复制。
这可能不是最好的编码方式,但它对我有效。
从github 获取最新的ZeroClipboard
在我的.cs.html文件中,我定义了按钮和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, 'rn');
//replace html break with line breaks
windowsText = windowsText.replace(/<brs*/?>/g, "rn");
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>
最新版本的零剪贴板使用event.stopImmediatePropagation,该事件在版本28.0之前的firefox中不存在,它失败并返回错误:
event.stopImmediatePropagation is not a function
你可以在这里看到浏览器比较:
http://compatibility.shwups-cms.ch/de/home?&property=TrackEvent.prototype.stopImmediatePropagation
我使用这个polifil添加缺少的功能:
https://github.com/mattcg/stopImmediatePropagation