...

View Full Version : window.onblur not working in IE



VorlonShadow
08-14-2008, 10:28 PM
I need to add a feature to my web app where when a user tries to click on another application, it will trigger a form submit (this is an online testing system). So, I'm using the jscript window.onblur event to accomplish this. In FireFox, it works very well, however, in IE, it does not (no surprises there). Here is my code:

<script type="text/javascript">
window.onblur = blurText;
function blurText() {
// remove the event to stop an infinite loop!
window.onblur = '';
alert("It's blured");
//document.f.submit();
}
</script>


As you can see, I've commented out the actual submit line because I'm testing. In IE7, the window (it's a pop-up window) comes up normally, but when I simply click on the page anywhere, the onBlur is triggered. Does anyone know what might be causing this, and if there is a solution to it? Or, even an alternate way to do what I need?

Thanks,
Jesse

tagnu
08-15-2008, 01:47 AM
works in IE 6.

VorlonShadow
08-15-2008, 12:31 PM
Doesn't in IE7, at least not on my machine.

rangana
08-15-2008, 12:44 PM
Works fine on IE7 and FF. Could you give us a link.

VorlonShadow
08-15-2008, 01:11 PM
Sure. Go to:

http://www.castleberryweb.com/dlg/test/dlg/

Log in with the user name 1234 and password qt

Click the Select link beside the "FINANCIAL MATH..." test. That should load the test into a pop-up window.

Now, the window should have focus, right? Now, just click anywhere on the page. You'll get a message, "It's blurred" But, it's not blurred, by clicking on the page, if anything, I'm just focusing back onto the page which should already have focus??

Thanks,
Jesse

VorlonShadow
08-18-2008, 10:58 PM
So, no one has any suggestions? :confused:

Thanks,
Jesse

VorlonShadow
08-18-2008, 11:20 PM
Here's a little piece of information. I went back to basics, and started with a plain page, nothing on it at all. standard html tags, no doctype tag at the top or anything. window.onblur worked perfectly in both IE and FireFox. Then, I started adding elements. on my second try, I duplicated the problem. If I include a DOCTYPE declaration, then the onBlur fails the way I describe above. Take DOCTYPE out, and it works just fine.

I really want my sites to be standard and correct, however, as usual, IE is not standards compliant. Does anyone have a suggestion for a workaround, or do I have to leave this page without a DOCTYPE declaration?

Thanks,
Jesse

VorlonShadow
08-19-2008, 12:06 AM
I believe I found a solution. Browsing around, I found a page, http://odondo.wordpress.com/category/computers/ that admitted that onBlur is not reliable, especially in IE. IE has it's own functions for this (of course), which tend to work better. So, I've changed my code to work like this:

<script type="text/javascript">
var isIE = (navigator.appName == "Microsoft Internet Explorer");
var hasFocus = true;
var active_element;

function setFocusEvents() {
active_element = document.activeElement;
if (isIE) {
document.onfocusout = function() { onWindowBlur(); }
document.onfocusin = function() { onWindowFocus(); }
} else {
window.onblur = function() { onWindowBlur(); }
window.onfocus = function() { onWindowFocus() }
}
}

function onWindowFocus() {
hasFocus = true;
}

function onWindowBlur() {
if (active_element != document.activeElement) {
active_element = document.activeElement;
return;
}
hasFocus = false;
document.f.submit();
}
</script>

And, so far, it seems to be working. Just thought I'd share that.

Thanks,
Jesse



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum