在Internet Explorer中,使用getData()和clearData ()来复制/清除剪贴板。
在Firefox和Safari,Opera,谷歌浏览器,使用execCommand的“Paste”和“Copy”命令复制内容到剪贴板。但由于浏览器的安全限制,使用execCommand的可能并不生效
为了避免安全限制的一个解决方案是使用Flash剪贴板操作,大家可以试试。
以下列出了几种常见Js复制代码,可跨浏览器复制内容
1、复制专题地址和url地址
<input type="button" name="anniu1" οnclick='copyToClipBoard()' value="复制本页title和url"> <script language="javascript"> function copyToClipBoard() { var clipBoardContent = ""; clipBoardContent += document.title; clipBoardContent += "\r\n"; clipBoardContent += this.location.href; //跨浏览器复制内容 copytext(clipBoardContent); }
2、复制url地址
<input type="button" name="anniu2" οnclick='copyUrl()' value="复制URL地址"> <script language="javascript"> function copyUrl() { var clipBoardContent = this.location.href; copytext(clipBoardContent); }
3、点击文本框时,复制文本框里面的内容
<input οnclick="oCopy(this)" value="你好.点击我copy内容!"> <script language="javascript"> function oCopy(obj) { var clipBoardContent = obj.value; //跨浏览器复制内容 copytext(clipBoardContent); }
4、复制文本框中的内容
<input id=imgurl type=text size=32 value="http://cssteach.com" /> <input type=button value="复制文本框中的内容" οnclick="CopyUrl(imgurl);" /> <script language="javascript"> function CopyUrl(obj) { var clipBoardContent = obj.value; //跨浏览器复制内容 copytext(clipBoardContent); } </script>
5、复制span标记中的内容
<span id="tbid">http://cssteach.com/</span>[<a href="#" οnclick="copyText(document.all.tbid)">点击复制</a>] <span id="tbid2">http://cssteach.com/1</span>[<a href="#" οnclick="copyText(document.all.tbid2)">点击复制</a>] <script type="text/javascript"> function copyText(obj) { var clipBoardContent = obj.value; //跨浏览器复制内容 copytext(clipBoardContent); }
核心方法:跨浏览器复制内容
//跨浏览器复制内容 function copytext(textToClipboard) { var success = true; if (window.clipboardData) { // Internet Explorer window.clipboardData.setData("Text", textToClipboard); } else { // create a temporary element for the execCommand method var forExecElement = CreateElementForExecCommand(textToClipboard); /* Select the contents of the element (the execCommand for 'copy' method works on the selection) */ SelectContent(forExecElement); var supported = true; //Firefox try { if (window.netscape && netscape.security) { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } // 复制 Firefo、Safari success = document.execCommand("copy", false, null); } catch (e) { success = false; } // remove the temporary element document.body.removeChild(forExecElement); } if (success) { alert("文本复制成功,可以粘贴了!"); } else { alert("您的浏览器不允许剪贴板访问!"); } } function CreateElementForExecCommand(textToClipboard) { var forExecElement = document.createElement("div"); // place outside the visible area forExecElement.style.position = "absolute"; forExecElement.style.left = "-10000px"; forExecElement.style.top = "-10000px"; // write the necessary text into the element and append to the document forExecElement.textContent = textToClipboard; document.body.appendChild(forExecElement); // the contentEditable mode is necessary for the execCommand method in Firefox forExecElement.contentEditable = true; return forExecElement; } function SelectContent(element) { // first create a range var rangeToSelect = document.createRange(); rangeToSelect.selectNodeContents(element); // select the contents var selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(rangeToSelect); }