IE6, firefox 点击复制到剪切板功能

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script>
            var id;
            function setCopy (id) {
                var con = document.getElementById(id).innerHTML;
                //window.clipboardData.setData("Text",con);
                if(window.clipboardData){
                    window.clipboardData.clearData();
                    window.clipboardData.setData("Text", con);
                    alert("广告代码复制成功!");
                } else if(window.netscape){     //Firefox
                    try{
                        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                    }catch(e){
                        alert("被浏览器拒绝!n请在浏览器地址栏输入'about:config'并回车n然后将'signed.applets.codebase_principal_support'设置为'true'");
                    }
                    var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
                    if(!clip){
                        return;
                    }
                    var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
                    if (!trans){
                        return;
                    }
                    trans.addDataFlavor('text/unicode');
                    var str = new Object();
                    var len = new Object();
                    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                    var copytext = con;
                    str.data = copytext;
                    trans.setTransferData("text/unicode",str,copytext.length*2);
                    var clipid = Components.interfaces.nsIClipboard;
                    if (!clip){
                        return false;
                    }
                    clip.setData(trans,null,clipid.kGlobalClipboard);
                    alert("广告代码复制成功!");
                }
            }
        </script>
        <body>
            <p ><xmp id="code"><a href="http://blog.163.com/ybbqg@126/blog/{$link}">请输入广告展示的内容!</a></xmp></p>
            <a href="http://blog.163.com/ybbqg@126/blog/#" onclick="setCopy('code');return false;" class="btn01" target="_self">复制此链接网址</a>
        </body>
</html>

posted @ 2012-03-16 08:30  前望  阅读(261)  评论(0编辑  收藏  举报