admin管理员组文章数量:1366332
I am using below mentioned javascript to copy the text to clipboard. Its working in IE, but not working in Firefox and Chrome.
Please advice me,What is wrong?
function setDataToclipboard()
{
var str=document.getElementById("populatedString").value;
if (window.clipboardData && clipboardData.setData) {
clipboardData.setData("Text", str);
alert("Copied!");
}
}
I am using below mentioned javascript to copy the text to clipboard. Its working in IE, but not working in Firefox and Chrome.
Please advice me,What is wrong?
function setDataToclipboard()
{
var str=document.getElementById("populatedString").value;
if (window.clipboardData && clipboardData.setData) {
clipboardData.setData("Text", str);
alert("Copied!");
}
}
Share
Improve this question
edited Jan 22, 2013 at 13:56
xlecoustillier
16.3k15 gold badges63 silver badges88 bronze badges
asked Jan 22, 2013 at 13:55
NajuNaju
1,5417 gold badges28 silver badges60 bronze badges
6 Answers
Reset to default 8The clipboard manipulation is not cross-browser. For cross-browser solution you need flash.
Look at this library https://github.com/jonrohan/ZeroClipboard
You can use ZeroClipboard like this:
<button id="my-button" data-clipboard-text="Copy me!">Copy to Clipboard</button>
<script>
var clip = new ZeroClipboard(document.getElementById('my-button'));
</script>
When you click on the button the text Copy me!
will be put into the clipboard.
For further instructions check the library's API https://github.com/jonrohan/ZeroClipboard/blob/master/docs/instructions.md
I think the window.clipboardData is IE only. Accessing the clipboard is a security concern, and thus cannot be done easily in FF or Chrome.
Please see this thread: How do I copy to the clipboard in JavaScript?
See the documentation for clipboardData
, specifically the section that reads:
There are no standards that apply here.
You are using proprietary Microsoft gubbins, so it shouldn't be expected to work on other browsers.
See this question for cross-browser techniques to access the clipboard.
There is a draft of a standard for accessing the clipboard but I'm not aware of any implementations of it in the wild (and canIuse doesn't know of any either).
w3c clipboard api is been implemented by all the browser http://caniuse.com/#feat=clipboard
I had this same problem with Chrome and other browsers recently. However, recently, I found this code works in a contenteditable field in certain browsers:
clipboard = e.originalEvent.clipboardData;
clipboard.setData('text/plain', plainData);
clipboard.setData('text/html', htmlData);
NOTE: e in this case is the copy and/or cut event. This event fires and is retrievable in an onCopy()
or onCut()
action.
This code is confirmed to work in the latest versions of the following browsers:
- Chrome (PCs/Macs and Android)
- Android 4.4+ WebView (as long as you update from the Play Store) -> good news for Android Devs
- Firefox
- Safari (Mac only)
Internet Explorer seems to work with window.clipboardData.setData
instead, but keep in mind that the IE clipboard will only accept 'text'
and 'url'
data.
While the following browsers can access the system clipboard object, these are unable to set data into the clipboard using clipboard.setData
:
- MS Edge
- gives an
UntrustedDragDrop
object into the clipboard instead... - also, setData returns true... when it doesn't work. setData returns undefined in all other browsers
- gives an
- Android WebView -> below 4.4
- iOS Safari and WebView - yay iOS!
I found that these do not work in Chrome or FF:
type="hidden"
<input id="e" type="hidden" value="my text to copy to clipboard" />
style="display:none;"
<input id="e" type="text" style="display:none;" value="my text to copy to clipboard" />
trick the browser with this
style="position: absolute; top: -30;"
<input id="e" type="text" style="position: absolute; top: -30;" value="my text" />
本文标签: javascriptcopy to clipboardnot working in FFchromeStack Overflow
版权声明:本文标题:javascript - copy to clipboard - not working in FF,Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738053670a2055895.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论