Avatar

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.

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 !

5071c5b861341c0dcfcf6ac86327701f

Tien Dung

August 19, 2008, August 19, 2008 00:41, permalink

No rating. Login to rate!

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);
}
A623216a5d2384489e012478c555d167

Juha Hollanti

August 20, 2008, August 20, 2008 11:19, permalink

No rating. Login to rate!

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);
}
Avatar

danielks

August 28, 2008, August 28, 2008 13:08, permalink

No rating. Login to rate!

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.

Db2af2c0fec84c164fd179790fdee227

Arvid Jakobsson

August 29, 2008, August 29, 2008 00:40, permalink

No rating. Login to rate!

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);
}

Your refactoring





Format Copy from initial code

or Cancel