/    Sign up×
Community /Pin to ProfileBookmark

Trapping window or body focus on browser focus…

Okay, here’s one I’m trying to solve without much success to date….

I have a web page which contains a couple different ActiveX controls running in Internet Explorer (let’s assume its version 6). One of those controls is a terminal emulator that has a green screen session to a mainframe.

All is well, the user can access the page, the terminal emulator loads, telnets them to the mainframe, even logs them in and brings them to a pre-determined place in the mainframe app automatically using some macros we built in and so on.

The problem arises when the user alt-tabs to another browser window or to another application altogether. When they alt-tab *back* to our browser window with the telnet session, the terminal emulator control no longer has focus. If the user types something, it doesn’t go into our control. The user has to click on the control for it to get focus before they can use it again.

This is more in the “annoying” category (imagine someone who uses this thing all day long), and I’m looking for a way to make the control get focus as soon as the browser window gains focus from the OS. This sounds like it should be straightforward, but in fact it isn’t.

First thing I tried was putting something in the BODY onFocus event, but this event does not seem to fire when the browser window regains focus. Next thing I tried doing is creating a simple function and assigning this to the window.onfocus event, and no joy here either. Essentially, here’s what I’ve been trying (scaled down for simplicity):

[CODE]<script type=”text/javascript”>

var objLogon ;
var objTelnet ;

function init()
{
objLogon = window.document.frmLogon ; // set global form ref
// do some stuff, set the objTelnet reference and log user in, etc..
window.onfocus = setTerminalFocus() ; // set onfocus event handler
objLogon.hdnLoaded.value = 1 ; // set form loaded flag
}

function setTerminalFocus()
{
if( window.document.frmLogon.hdnLoaded.value == 1 ) {
objTelnet.focus() ;
}
}

</script>
<body onLoad=”javascript:init();”>
<form name=”frmLogon”>
<input type=”hidden” name=”hdnLoaded” value=”0″>
[/CODE]

The solution for this may well be simple and I’m just missing it, but if you know a way to solve this problem then please let me know!

Cheers,
Fidel

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 04.2005 — 
objTelnet.focus() ;
[/quote]

I see no object defined as objTelnet
Copy linkTweet thisAlerts:
@nzakasMay 04.2005 — You really want to set an onfocus() event handler:

[CODE]window.onfocus = function () {
document.getElementById("your_telnet_id").focus();
};[/CODE]


Alternately:

[CODE]<body onfocus="document.getElementById('your_telnet_id').focus();">[/CODE]
Copy linkTweet thisAlerts:
@Comrade_FidelauthorMay 05.2005 — You really want to set an onfocus() event handler:

[CODE]window.onfocus = function () {
document.getElementById("your_telnet_id").focus();
};[/CODE]


Alternately:

[CODE]<body onfocus="document.getElementById('your_telnet_id').focus();">[/CODE][/QUOTE]


Hi Nick, thanks for your response.

Is there any reason why I can not use the globally defined reference I have as objTelnet? This is defined in the init() function.

Also, the problem I have is not about getting a handle on the telnet object, but in responding to the browser gaining focus from another application.

The window.onfocus event does not fire when the browser application regains focus. You'll see in the code snippet that I am setting window.onfocus to a predefined function, but this is not getting run. I also tried this in Mozilla to see if it was a browser dependent issue, but no joy there either.

Any ideas would be much appreciated.

Cheers,

Fidel
×

Success!

Help @Comrade_Fidel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...