Click to See Complete Forum and Search --> : onkeyup issue in IE6


JPnyc
06-09-2006, 12:22 PM
Yeah I know I'm the admin here but I thought I'd make use of the extensive knowledge base that is our membership.

I've run into an IE quirk that I wondered if any of you had encountered before. The reader's digest version is, if you have a function which alters an HTML doc, such as changing text, either with DOM methods or MS's own innerHTML (both tested), and the function is called onkeyup event, it breaks the undo function in IE (this presupposes the page contains a textarea, obviously).

The function will still work as expected, however undo will not, and only in IE6. Firefox and Opera are not affected, and throw no errors either. IE7 was not tested. Any of you ever run into this?

phpnovice
06-09-2006, 01:43 PM
Do you have a sample HTML and JavaScript setup which I can try out?

JPnyc
06-09-2006, 02:10 PM
No, but I could whip one up. Someone else on another forum tested this on his own, and told me the result was the same but i never saw their coding. I'll whip up a test and post it. Thanks.

JPnyc
06-09-2006, 02:21 PM
Here, or if you prefer I'll attach it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--

-->
</style>

<script type="text/javascript">
<!--

var maxchars=2500;
function stringLen() {
var currLength=document.getElementsByTagName('textarea')[0].value;
var charsLeft= (maxchars - currLength.length);

document.getElementById('chars').firstChild.data='CharsLeft: '+charsLeft;
}


//-->
</script>
</head>
<body>
<span id='chars'style="color:#000;font:normal 12pt verdana">CharsLeft: </span>

<form action="">

<textarea name="message" id="message" rows="20" cols="60" onkeyup="stringLen()"onmouseup="stringLen()"></textarea>


</form>
</body>
</html>

phpnovice
06-09-2006, 02:41 PM
Yep, you're correct -- definitely an interesting bug. I tested every alternative method for referencing the different elements and properties (even other events, onkeydown and onkeypress). The result is that undo
(Ctrl-Z) always fails if the document has been modified via DHTML.

JPnyc
06-09-2006, 02:44 PM
Thanks man, that's what I was afraid of. I, just like you did, tried changing the method of referencing the textarea, I went to non-DOM methods (figuring it might like MS methods better) and I even changed the key event. It's some flaw in the way IE handles keyboard events. I appreciate your time. Thanks-Joe

phpnovice
06-09-2006, 02:46 PM
No problem.

Cheers.

JPnyc
06-09-2006, 03:54 PM
Just a FYI on this, I'm wrong about the keyboard events, calling the function on mouse events also breaks undo. I get the feeling undo just will not work in IE6 jf any DHTML appending of the doc is done.

phpnovice
06-09-2006, 04:04 PM
Hmmm... That would be an interesting test. Don't use any events at all. Set up an interval timer to check the textarea length after a delay of 10 seconds or so. This would give you time to type something undo it (to verify undo is currently working) and start typing again. When the counter starts to change, try the undo again and see if it is now broke. ;)

JPnyc
06-09-2006, 04:13 PM
Yep, confirmed. I set it to run every 2 secs, and sure enough as soon as it does, undo is history. What's also odd is, if I called the function just once from a timed event, and then I typed more AFTER it ran, the undo would function on the newly typed text. So apparently it doesn't break it for the entire session, just what's on the page when the function runs.

phpnovice
06-09-2006, 04:14 PM
Yep, I figured that would be the case. It's like it "commits" the current changes each time you change the document via DHTML.

JPnyc
06-09-2006, 04:26 PM
Apparently wherever IE stores the previous state(s) of text it's being being cleared by the function. That's pretty bad design, if what I'm theorizing is even close to accurate.

phpnovice
06-09-2006, 04:29 PM
Just my opinion, but I would not call this a design flaw. I'd call it a bug -- plain and simple.

JPnyc
06-09-2006, 04:33 PM
Either works for me. I'm having someone test in IE7 beta. Curiousity is aroused now.

edit: It breaks undo in IE7 beta as well. :(

edit2: I also tried different doctypes, as well as NO doctype to see what quirksmode would yield. All the same. I was hoping it was some oversight in my code.

mrhoo
06-09-2006, 04:51 PM
I'm watching this thread with interest- I had the same problem,
also in IE only, with a 'code console' text area.
The System undo Array seems to restart itself
in IE whenever the document containing the textarea changes.

The only way to cure it seems to be to put the textarea in its own
window- an iframe will do, positioned where the textarea is supposed to go.

But that means more code writing to and from the textarea, plus the
iframe creation and styling...and sniffing for IE...

I ended up with a 'command history' array, logging the textarea value string onchange, and a 'roll back' function button. But it isn't control-z!

JPnyc
06-09-2006, 05:10 PM
Wow, that's more work than this stupid little feature could ever be worth.

mrhoo
06-09-2006, 05:19 PM
More work? You should have seen me trying to shift() a saved array of commands into the textarea after the code that changed the page ran,
so I could undo them again... it got ugly.

I regained my sanity just as I was contemplating an object that would
fake and dispatch each textarea 'version' as a series of keystrokes...

JPnyc
06-09-2006, 05:23 PM
I gather you were forced to do this for some employer? Because to have to do all that to compensate for an IE bug.........................I mean we ALL compensate for IE on a relatively SMALL scale but geez. I would convince my boss the feature wasn't that important before going through all that work.