将图像数据复制到剪贴板?

时间:2010-08-09 21:02:26

标签: javascript

我正在一个网站上,客户在产品页面上有优惠券代码图片,当用户点击图片时,他希望将代码复制到剪贴板中......这可能吗?

2 个答案:

答案 0 :(得分:0)

使用JavaScript剪贴板访问几乎在所有浏览器中禁用,除非用户在其设置中启用它。解决方法是使用Flash Copy到剪贴板脚本,但Flash正在设置更多的安全限制。查看zeroclipboard

答案 1 :(得分:0)

如果要复制图像,可以像这样使用“createControlRange()”:

var imageTag = document.getElementById("couponCode");
if (document.body.createControlRange) //to check if browser is IE
{
    var couponRange;
    document.getElementById("couponCode").contentEditable = true;
    couponRange = document.body.createControlRange();
    couponRange.addElement(imageTag);
    couponRange.execCommand('Copy');
    document.getElementById("couponCode").contentEditable = false;
    alert("Image copied");
}
else //for other browsers
{
    alert("Your browser does not allow access to clipboard.\nPlease press Ctrl+C to copy");
    var couponRange = document.createRange();
    couponRange.selectNode(imageTag);
    window.getSelection().removeAllRanges();
    window.getSelection().addRange(couponRange);
}

IE之外的浏览器出于安全原因不允许Javascript访问剪贴板。但是,您可以通过要求用户在选择优惠券图像的范围后按Ctrl + C来调用浏览器功能。

希望这能解决你的问题。

相关问题