日期:2014-05-20  浏览次数:20733 次

我写的一个简单的js 为啥在火狐下不好使 请高手指教!!!!

<script language="JavaScript" type="text/JavaScript">
function copyToClipBoard(){
var clipBoardContent='';
clipBoardContent+=window.location;
window.clipboardData.setData("Text",clipBoardContent);
alert("你已复制链接,请粘贴到QQ/MSN等发给好友!");
}
</script>


<a href="#" onclick="copyToClipBoard()">复制URL和标题发给好友</a>

上面是我写的复制URL的js脚本
请热心善良的高手们帮小弟解决下这个问题。我好想知道如果在其他浏览器下复制URL是怎么写的,求知强烈。[color=#FF0000][/color]




------解决方案--------------------
试试这段代码,在文本框输入你要的地址!
<script type="text/javascript">
function copy_clip(copy){
if (window.clipboardData){
window.clipboardData.setData("Text", copy);}
else if (window.netscape){
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
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=copy;
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("已复制"+copy)
return false;
}
</script>
<h1>请另存代码测试</h1>
<input type="text" id="ff" value="" /><button onclick="copy_clip(document.getElementById('ff').value)">复制
</button>
------解决方案--------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
<TITLE> New Document </TITLE>
 </HEAD>

 <BODY>
<script language="JavaScript" type="text/JavaScript"> 
function copyToClipBoard(){ 
copy_clip(window.location);
alert("你已复制链接,请粘贴到QQ/MSN等发给好友!"); 


function copy_clip(text){
if (window.clipboardData){ // IE
window.clipboardData.setData("Text", copy);}
else if (window.netscape){ // FF
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
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=text;
str.data=copytext;
trans.setTransferData("text/unicode",str,copytext.length*2);

var clipid=Components.interfaces.nsIClipboard;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
}
</script> 


<a href="#" onclick="copyToClipBoard()">复制URL和标题发给好友 </a> 

 </BODY>
</HTML>

------解决方案--------------------