WalkingSmall

… what’s the difference?

The javascript monster strikes back

August23

Monster
And then there was silence… or just a new piece of javascript crap that doesn’t work like it should! The main problem this time is the onfocus event and its good friend the focus() trigger. The story began with the following lines of code…

document.getElementById(’inputfield’).onfocus = function(){ alert(’focused’) };
document.getElementById(’inputfield’).focus();

Normally you’d expect the focus() function to trigger the onfocus event. What really happened is nothing. The trigger does nothing. By the way, the input element was focused as it should… After 5 minutes of throwing some bad words against my screen, I’ve decided to find a clue for this problem. First I tried moving the focus() trigger to the body onload function… but nothing happened. Then I moved the focus line to the bottom of the script right before the <body> tag and again nothing happened. Again some bad word minutes followed… Then I tried to trigger the focus with my friend firebug and its console. Same line of code, except that it was triggered in runtime… and tadaa the alert window pops up to tell me that it’s focused now. Yehaaw I thougt, it’s not impossible so far.
I started writing some despaired code like:

document.getElementById(’inputfield’).focus();
document.getElementById(’inputfield’).blur();
document.getElementById(’inputfield’).focus();

Of course it didn’t work… but hey, it was dumb enough to work. After 10 minutes of googling for any solution, I only found some javascript kiddies asking how to get focus on form elements. But then the holly grail comes right by! What if I try to trigger the focus just a bit later than the onload?! And how do you trigger something a bit later? ? ? Timeout, right! So I wrote the following (yep really dumb) line of code into the onload function:

setTimeout(’document.getElementById(\’inputfield\’).focus();’, 100);

… and … woohoo, focus baby!! Yeah I know, 100 milliseconds can be a long time if you want to load a page fast and on time. So I set the timeout a bit lower… 10 works good, but mighty and strong as I was (not really), I’ve risked it… 5 works fine… but the winner is… 1 … yes 1 milliseconds does it all. It works for me, but surely it’s not the nicest way of focusing an element. What ever, it takes me at least 30 minutes and maybe it helps you saving some minutes of your life…

A new beginning

August20

a new beginning

After barely 3 months of posting nothing, I decided to give this blog thing a new go. First step was to find a new blog software. So found in Wordpress (nice work by the way). Second step was to create a new layout… Tadaa, there it is. Next step is to write more constantly stuff down here… I’ll do my best!

posted under Me vs. ? | No Comments »

Archiv