• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 532
  • Last Modified:

window.focus() and iframe

Hi,

I have a chat script which uses window.focus() when there is a new message to highlite the window in the taskbar. It works fine when I use a popup. But when I put the chat application in an iframe and then window.focus() I have a problem with typing in the text. It constantly focuses to the begin of the input field. So when a user sends a message and I type a message then my cursor goes to the beginning of the input field. Is there a way to focus to the iframe and that the window will highlite in the taskbar? Or is there a way to only window.focus() when the window is not active (so when I'm not typing)?

This is what I have now:

window.focus();
document.forms['chatForm'].elements['chatbarText'].focus();
0
Tagor
Asked:
Tagor
  • 3
1 Solution
 
NicksonKohCommented:
Give your iframe a name.

E.g. <iFrame name="ifChild" ...></iFrame>

Then in your js do focus as follows

ifChild.document.forms['chatForm'].elements['chatbarText'].focus();
0
 
TagorAuthor Commented:
This doesn't work cause the Javascript is in the iframe. By the way it does focus on the input field. But it first focuses on the window and then on the input field which means that if you are typing text it puts the cursor in from of the typed text when a message is received.
0
 
NicksonKohCommented:
Hi Tagor,

I have not this before. Check out the code below. It is exactly fantastic but seems to work. Below is the code for the Parent Page which I call Test.asp. In the Code Snippet is Test2.asp.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

    <script type="text/javascript" language="javascript">
    </script>

</head>
<body>
Parent Window
    <iframe src="Test2.asp"></iframe>
</body>
</html>


<html>
<script>
    window.onblur = function(timer, time) {
        clearTimeout(timer);
        timer = setTimeout("window.focus();document.getElementById('Text1').focus()", 1000);
    }
</script>
<body onload="window.focus();document.getElementById('Text1').focus()">
Frame 2
    <input name="Text1" id="Text1" type="text" /></body>
</html>

Open in new window

0
 
NicksonKohCommented:
I mean I have not done this before.

Anyway check it out. Note the code restores focus every 1s. You can change that, but I thought it can be irritating.

cheers
Nickson
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now