window.focus() and iframe

Posted on 2009-04-15
Medium Priority
Last Modified: 2013-11-05

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:

Question by:Tagor
  • 3
LVL 17

Expert Comment

ID: 24153984
Give your iframe a name.

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

Then in your js do focus as follows


Author Comment

ID: 24158881
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.
LVL 17

Accepted Solution

NicksonKoh earned 720 total points
ID: 24164452
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">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

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

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

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

Open in new window

LVL 17

Expert Comment

ID: 24164457
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.


Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question