This is an age-old question, but I'm still having trouble with it. You see, I'm trying to paste some Excel data in a Text Area, but the silly browsers freeze for long periods of time when doing this, because of God-knows-what "smart" parsing they do. I can't get rid of that (file upload is out of the question, my boss wants me to paste rows from Excel in a Text Area).
The good news is that pasting in a standard textbox WORKS. But I can't force them to paste there. So I am trying to catch the paste event in the Text Area and then throw the text over to the textbox. Unfortunately, I stopped short at the pasting part. I can't paste the text via JS into the simple textbox.
So my question is: how do you paste a text, how do you invoke it via JS? There are some solutions which only work in IE, that's not good, of course ::- ).
This is an age-old question, but I'm still having trouble with it. You see, I'm trying to paste some Excel data in a Text Area, but the silly browsers freeze for long periods of time when doing this, because of God-knows-what "smart" parsing they do. I can't get rid of that (file upload is out of the question, my boss wants me to paste rows from Excel in a Text Area).
The good news is that pasting in a standard textbox WORKS. But I can't force them to paste there. So I am trying to catch the paste event in the Text Area and then throw the text over to the textbox. Unfortunately, I stopped short at the pasting part. I can't paste the text via JS into the simple textbox.
So my question is: how do you paste a text, how do you invoke it via JS? There are some solutions which only work in IE, that's not good, of course ::- ).
Share Improve this question edited Dec 3, 2014 at 21:26 Dmitry Pashkevich 13.6k10 gold badges58 silver badges75 bronze badges asked Jan 26, 2010 at 20:11 AxonnAxonn 10.3k6 gold badges34 silver badges44 bronze badges 2- The regular textbox is faster because it only holds one line (duh). – Josh Stodola Commented Jan 26, 2010 at 20:23
- Duh of course. But it still pastes Excel data ok, separated by tab. – Axonn Commented Jan 26, 2010 at 21:19
5 Answers
Reset to default 6Simple.
var isIe = (navigator.userAgent.toLowerCase().indexOf("msie") != -1
|| navigator.userAgent.toLowerCase().indexOf("trident") != -1);
document.getElementById('textinput').addEventListener('paste', function(e) {
var text;
if (isIe) {
text = window.clipboardData.getData('Text')
} else {
text = e.clipboardData.getData('text/plain');
}
// Do whatever you want with the text
console.log(text);
//If you don't want the text pasted in the textarea
e.preventDefault();
});
<textarea id="textinput"></textarea>
If you want, you can even get rid of the textarea and do this more directly. I wrote a technical blog post explaining how we do copying and pasting at Lucidchart (where I work).
Sorry, didn't quite catch the idea. Can't you attach to thextarea's onpaste
event (at least I know IE has such event) and then simply set textarea's value to the pasted value?
pastedContent = window.clipboardData.getData("Text");
document.getElementById("yourtextarea").value = pastedContent;
EDIT: ok, it seems like this only works in IE and newer versions of FF, but it's not a cross-browser solution.
I can't paste the text via JS into the simple textbox
When you say “simple textbox”, do you mean <input type="text">
? If so, then I think setting its value
attribute to the text you’ve captured from the <textarea>
should work.
Enable javascript Copy to Clipboard in Firefox or Mozilla: http://www.febooti./support/website-help/website-javascript-copy-clipboard.html
Try CodeMirror as alternate solution. Doesn't check it with copy&paste with huge/excel amount of data but maybe this help...
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744204045a4563013.html
评论列表(0条)