Forum Moderators: open

Message Too Old, No Replies

Safari & OnKeyPress

Is this combo non-existent?

         

mattglet

7:15 pm on May 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to get an OnKeyPress event fired in Safari v1.0.3, and it's not working (it still fires the event after you return false). I've researched a little bit, only to find sparce mentionings.

Does Safari not use the OnKeyPress event correctly?

tedster

9:42 pm on May 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From what I've read, Safari has bugs in all three events: "onkeydown", "onkeyup" and "onkeypress". There are complaints in many developer forums, and the problems are apparently not even consistent across various versions of Safari, which makes matters much worse.

mattglet

4:26 pm on May 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can anyone provide a sensible workaround for this?

I'm trying to disable input into a text field (myField) when a user tries to type in it. The solution cannot entail pre-setting myField as disabled, because the ability/inability to enter data into myField happens on the fly, based on other dropdown selected values, etc.

whoisgregg

9:43 pm on May 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not use document.getElementById('theinput').disabled = true; when the appropriate dropdowns are selected?

4specs

1:00 am on Jun 1, 2005 (gmt 0)

10+ Year Member



I went back and checked after reading this posting. We use onmousedown to trigger a script to log the referral. We have about the right number of referrals from Safari browsers and do not suspect a problem.

mattglet

2:29 am on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Without getting into too many specifics, I don't have control over the dropdowns when they are selected.

I did, however, come up with a semi-workaround for onkeypress. Here's some incomplete code, just to give an idea:

<script type = "text/javascript">
function doWhatever() {
if (document...selected.value == 0) {
if (navigator.userAgent.indexOf('Safari') >= 0) {
document.form[0].something.value = '';
document.form[0].something.disabled = true;
alert('You can't type here, blah blah blah.');
document.form[0].something.disabled = false;
}else{
... do regular onkeypress validation stuff
}
}
}
</script>

<input type = "text" name = "something" onkeypress = "doWhatever();">

At first glance, you'll ask how in the world this is the solution to my above problem.

Well, you first have to make sure you don't want to type in the input field. Then you disable the field, and throw your alert. After the user ok's the alert, Safari will try to write to the field, but since it's disabled, it will not succeed (this all happens before Safari gets to the next line of code). After Safari does its thing, and tries unsuccessfully to write to the field, you can enable the field again.

Yes, it may not make sense, but it does in fact work. Try it out. I don't pretend to really know why it works, I've long since passed trying to understand the nuances of Safari.

whoisgregg

6:58 pm on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't pretend to really know why it works, I've long since passed trying to understand the nuances of Safari.

Funny, I develop in Safari and say the same thing with one change:

I don't pretend to really know why it works, I've long since passed trying to understand the nuances of Internet Explorer. :)

mattglet

8:33 pm on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Touché ;)

I primarily use Opera, so I definitely find the humor in that