Electricity Suppliers | Loans | Loans | Mortgage Calculator | Boston Sports Tickets
Javascript Event Handling Weirdness [Archive] - ZGeek

PDA

View Full Version : Javascript Event Handling Weirdness


dwarfthrower
18-01-2005, 09:33 AM
Howdy, I was wondering whether anyone else had run across this little gem of strangeness. In IE, some events don't seem to be handled if another event fires beforehand.

<html>
<head>
<script>
function do_link_onclick(){
alert("do_link_onclick() fired");
}
function do_field_onchange(){
alert("do_field_onchange() fired");
}
</script>
</head>
<body>
<textarea onchange="do_field_onchange()">Change this value</textarea>
<br>
<a href="#" onclick="do_link_onclick()">Now Click Here</a>
</body>
</html>

In Firefox, if you edit the value in the textarea, then click on the link, you get the notification of the onchange event firing, then the notification of the link's onclick event firing. Do the same in IE and only the textarea's onchange event fires.

Has anybody got any ideas about how to get IE to behave rationally in this instance?

Asmodeus
18-01-2005, 09:45 AM
in IE, when you go and click on the link, you finally cause the first object to lose focus which is what finally fires teh event.

after each thing there, add ;return true and it will work as expected. this is an old habit that recently seems to have fallen out of practice with many people.

thusly:

<html>
<head>
<script>
function do_link_onclick(){
alert("do_link_onclick() fired");
}
function do_field_onchange(){
alert("do_field_onchange() fired");
}
</script>
</head>
<body>
<textarea onchange="do_field_onchange();return true">Change this value</textarea>
<br>
<a href="#" onclick="do_link_onclick();return true">Now Click Here</a>
</body>
</html>

dwarfthrower
18-01-2005, 10:13 AM
Cheers Asmo, but no joy I'm afraid. Using IE6 on Win2K if that helps. I also tried a call to return true at the end of each function without any success.

Edit... it seems that the alerts themselves are buggering up the code... compare:

<html>
<head>
<script type="text/javascript">
function do_link_onclick(){
document.getElementById('wibble').value += '\ndo_link_onclick() fired';
}

function do_field_onchange(){
document.getElementById('wibble').value += '\ndo_field_onchange() fired';
}
</script>
</head>
<body>
<textarea onchange="do_field_onchange()">Change this value</textarea><br>
<a href="#" onclick="do_link_onclick()">Now Click Here</a><br>
<textarea cols="50" rows="10" id="wibble"></textarea>
</body>
</html>


Both events fire.

Asmodeus
18-01-2005, 05:08 PM
fucking windows. :)

i tested and ran that on win xp sp2 with newest IE. worked fine there.

dwarfthrower
18-01-2005, 05:14 PM
No worries... I got around it using onkeypress rather than onchange... tiny performance hit running my script more often than I really have to, but hey.. it works.

Thanks for testing things out for me.