I wrote a little javascript function that checks for a cookie when onFocus is triggered in a form input text field.
If the cookie is found, then a dialog pops up welcoming the person back to the site.
But in Chrome, each time a dialog is closed a new one pops up. I suppose it’s because the text field is still technically ‘Focused’.
But this problem does not occur in Firefox or IE9.
I solved it in Chrome by adding in the lines that I currently have commented. But what I would like to know is if this behavior is considered to be a bug or normal behavior, and if it is normal then why do the other browsers behave differently?
Here is the javascript code sample:
//var checked = 0;
var checkIt = getCookie('cia_db');
function checkCookie() {
// if(checked == 0) {
if(checkIt == 'logged_in') {
alert('Welcome back to CIA headquarters.');
//return true;
}
//}
//checked++;
}
And here’s the html:
<form id="form1" name="form1" action="" method="get">
<input type="text" name="username" id="username" value="smeegle" size="15" onfocus="checkCookie();">
etc…
The field will lose the focus() when the alert appears, chrome gives the focus back to the input when the alert is closed, so onfocus fires again.
Other browsers may give the focus to the window after closing the alert, so onfocus will not fire again.
I wouldn’t call it bug, it’s a different behaviour.
Solution:
Remove the focus before the alert()