window.focus() and iframe

Posted on 2009-04-15
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
    LVL 17

    Expert Comment

    Give your iframe a name.

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

    Then in your js do focus as follows

    LVL 4

    Author Comment

    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

    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

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (, typical array handling might look like this: (CODE) B…
    This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
    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…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now