1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//msg is the string to show in the alert. //obj is the input to focus function msgFocus(msg, obj){ var e_keyup = obj.onkeyup; var e_keypress = obj.onkeypress; var e_blur = obj.onblur; var e_focus = obj.onfocus; obj.onkeyup = null; obj.onkeypress = null; obj.onblur = null; obj.onfocus = null; alert(msg); setTimeout(function() { obj.focus(); obj.onkeyup = e_keyup; obj.onkeypress = e_keypress; obj.onblur = e_blur; obj.onfocus = e_focus; }, 50); }
Refactorings
No refactoring yet !
Tien Dung
August 19, 2008, August 19, 2008 00:41, permalink
Hi Daniel,
I create a saveAndSet function to remove repetitions. Don't have time to test. Hope it work :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
function saveAndSet(obj, newValues) { var oldValues = {}; for (var p in newValues) if (newValues.hasOwnProperty(p)) { oldValues[p] = obj[p]; obj[p] = newValues[p]; } return oldValues; } function msgFocus(msg, obj) { var oldValues = saveAndSet(obj, {onkeyup: null, onkeypress: null, onblur: null, onfocus: null }); alert(msg); setTimeout(function() { obj.focus(); saveAndSet(obj, oldValues); }, 50); }
Juha Hollanti
August 20, 2008, August 20, 2008 11:19, permalink
Form elements can be disabled. Ofcourse in this case the focus is set after enabling the element. But would that suit your needs? I was just curious, why do you need to disable the events?
1 2 3 4 5 6 7 8 9 10
//msg is the string to show in the alert. //obj is the input to focus function msgFocus(msg, obj){ obj.disabled = true; alert(msg); setTimeout(function() { obj.disabled = false; obj.focus(); }, 50); }
danielks
August 28, 2008, August 28, 2008 13:08, permalink
I disable the events because sometimes weird stuff happens when the control is focused again and it was the easiest solution :)
For exemple, I do some things on the KeyPress event, and when the alert pops up, if the user hit space to close it, it will be captured on the event, and that is not good (at least in my specific case).
And thanks for the help guys.
Arvid Jakobsson
August 29, 2008, August 29, 2008 00:40, permalink
This might work, haven't tested. Extends the HTMLElement prototype to give it a dis/enableHandlers method.
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
HTMLElement.prototype.disablehandlers = function () { for (var i = 0; handler = 'on' + events[i]; i++) { this['_' + handler] = this[handler]; this[handler] = null; } } HTMLElement.prototype.disablehandlers = function (events) { for (var i = 0; handler = 'on' + events[i]; i++) this['_' + handler] = this['_' + handler]; } function msgFocus(msg, obj) { events = ['keyup', 'keypress', 'blur', 'focus']; obj.disableHandlers(events); alert(msg); setTimeout(function() { obj.focus(); obj.enableHandlers(events); }, 50); }
I don't know why, after an alert, if I set focus on an input on Firefox, it just doesn't focus. So I created a little function, which saves all events, sets them to null, shows the alert, set a timeout, focus, then restores the events. I wanted a better way to save, set null, then restore the events, since it has repetitions.