兼容Firefox的点击复制js代码
<script type="text/javascript">
function copyCode(id){
var testCode=document.getElementById(id).value;
if(copy2Clipboard(testCode)!=false){
alert("生成的代码已经复制到粘贴板,你可以使用Ctrl+V 贴到需要的地方去了哦! ");
}
}
copy2Clipboard=function(txt){
if(window.clipboardData){
window.clipboardData.clearData();
window.clipboardData.setData("Text",txt);
}
else if(navigator.userAgent.indexOf("Opera")!=-1){
window.location=txt;
}
else if(window.netscape){
try{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch(e){
alert("您的firefox安全限制限制您进行剪贴板操作,请打开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试,相对路径为firefox根目录/greprefs/all.js");
return false;
}
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=txt;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);
}
}
</script>
<div>
<a href="#" target="_blank"> [<a href="#" onclick="copyCode('txtTestCode1');return false;">点击复制1</a>]<br>
<textarea rows=2 id="txtTestCode1" style="500px;font-family: Verdana, Arial;font-size:12px;height:55px;border:1px solid #999999;">复制内容1</textarea>
</div>
<br><br>
<div>
<a href="#" target="_blank"> [<a href="#" onclick="copyCode('txtTestCode2');return false;">点击复制2</a>]<br>
<textarea rows=2 id="txtTestCode2" style="500px;font-family: Verdana, Arial;font-size:12px;height:55px;border:1px solid #999999;">复制内容2</textarea>
</div>
<br><br>
<div>
<a href="#" target="_blank"> [<a href="#" onclick="copyCode('txtTestCode3');return false;">点击复制3</a>]<br>
<textarea rows=2 id="txtTestCode3" style="500px;font-family: Verdana, Arial;font-size:12px;height:55px;border:1px solid #999999;">复制内容3</textarea>
</div>
<br><br>
1、复制专题地址和url地址
<input type="button" name="anniu1" onClick='copyToClipBoard()' value="复制地址">
<script language="javascript">
function copyToClipBoard(){
var clipBoardContent="";
clipBoardContent+=document.title;
clipBoardContent+="";
clipBoardContent+=this.location.href;
window.clipboardData.setData("Text",clipBoardContent);
alert("复制成功!");
}
</script>
2、复制url地址
<input type="button" name="anniu2" onClick='copyUrl()' value="复制URL地址">
<script language="javascript">
function copyUrl()
{
var clipBoardContent=this.location.href;
window.clipboardData.setData("Text",clipBoardContent);
alert("复制成功!");
}
</script>
3、点击文本框时,复制文本框里面的内容
<input onclick="oCopy(this)" value="你好.要copy的内容!">
<script language="javascript">
function oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy")
alert("复制成功!");
}
</script>
4、复制文本框或者隐藏域中的内容
<script language="javascript">
function CopyUrl(target){
target.value=myimg.value;
target.select();
js=myimg.createTextRange();
js.execCommand("Copy");
alert("复制成功!");
}
function AddImg(target){
target.value="[IMG]"+myimg.value+"[/ img]";
target.select();
js=target.createTextRange();
js.execCommand("Copy");
alert("复制成功!");
}
</script>
<input name=myimg type=hidden id=myimg value="http://pmp.cnblogs.com/" />
<input name=imgurl type=text size=32 value="http://pmp.cnblogs.com/" />
<input type=button value="点击这里复制本站地址" onclick="CopyUrl(imgurl);" />
5、复制span标记中的内容
<script type="text/javascript">function copyText(obj)
{
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
alert("复制成功!");
}
</script>
以下是代码片段:<br />
<br />
<span id="tbid">http://pmp.cnblogs.com</span>
[<a href="#" onclick="copyText(document.all.tbid)">点击复制</a>]<br/><br/>
<span id="tbid2">http://www.cnblogs.com/pmp</span>
[<a href="#" onclick="copyText(document.all.tbid2)">点击复制</a>]<br/><br/>