You are here: DEVPPL Forum Programming JavaScript Forum
NOTIFICATIONS
54.314
MEMBERS
15.719
TOPICS
62.396
POSTS
  562
FLASH GAMES
7.740
TUTORIALS
 

Login

E-mail:
Password:

Trouble with focus() in javascript

0

Loading

Trouble with focus() in javascript

Postby Axsagb » Mon May 23, 2011 7:16 pm

Hello,

I am researching an issue in a rather big app which displays pdf files in IE window.
The issue is that inside javascript code we call 'window.setTimeout( win.focus() ...)' (for the child window), but it doesn't come into focus every time, seems a bit random when it does and does not come into focus.

At present I wrote a much smaller app + javascript to reproduce the issue, but it does not manifest in same way.
Specifically this is the confusing part:
window.setTimeout( function() {win.focus(); win.moveTo( 200 , 200 ); } , 500 , "JavaScript" );
the 'win' does move, but never comes into focus.

Any ideas about why the focus function does not accomplish what I'm trying to do?

Here's the code of my simplified app + html + javascript:

JAVASCRIPT:
Code: Select all
var win;

function f(s) {
       website="http://machine/apache/jsfnu/mytest" + s + ".txt";
       if (!win)
           win = window.open( website , "thetest" , "toolbar=yes,resizable=yes");
       else
       {
           win.url = website;
       }
       win.navigate(website);
       win.focus();
       window.setTimeout( function() {win.focus(); win.moveTo( 200 , 200 ); } , 500 , "JavaScript" );
   }
   

function emitApplet() {
document.write("<APPLET CODE=\"mytest.class\" archive=\"mytest.jar,netscape.jar\" NAME=\"myApplet\"  MAYSCRIPT HEIGHT=1000 WIDTH=1000> </APPLET>");
}

function window_onUnload()
{
}


JAVA code:
Code: Select all
import netscape.javascript.*;
import java.awt.*;
import java.awt.event.*;
public class mytest
extends java.applet.Applet implements ActionListener
{
    Button nextButton;
    Button prevButton;

    int _page=1;
    public void init() {
        System.err.println("init started");
        setLayout(new FlowLayout());
        System.err.println("setLayout done");
        nextButton = new Button("Next!");
        System.err.println("next button created");
        prevButton = new Button("Prev!");
        System.err.println("prev button created");
        add(prevButton);
        System.err.println("prev button added");
        add(nextButton);
        System.err.println("next button added");

        nextButton.addActionListener(this);
        System.err.println("next button action");
        prevButton.addActionListener(this);
        System.err.println("prev button created");
    }
    public void actionPerformed(ActionEvent evt)
    {
        if (evt.getSource() == nextButton)
        {
            doButton(++_page);
        }
        else if (evt.getSource() == prevButton)
        {
            doButton(--_page);
        }
    }
    public void doButton(int page)
    {
        JSObject win = JSObject.getWindow(this);
        JSObject doc = (JSObject) win.getMember("document");

        JSObject loc = (JSObject) doc.getMember("location");
        String s = (String) loc.getMember("href");
        String []args = new String[1];
        args[0] = (new Integer(page)).toString();
        win.call("f", args);
    }
    public void paint (java.awt.Graphics g)
    {
        g.drawString("Hello, World9!",50,25);
    }
}


HTML:
Code: Select all
<script type="text/javascript" src="StartTest.js"> </script>
<html>
        <head>
                <title>try</title>
        </head>
        <body bgcolor="#fdf8ed" text="black" language="Javascript" onload="window_onUnload()">

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

                                        emitApplet();

                        </script>

                </center>
        </body>
</html>
Axsagb
 
Reputation: 0
Posts: 1
Joined: Mon May 23, 2011 7:13 pm
Highscores: 0
Arcade winning challenges: 0

Trouble with focus() in javascript - Sponsored results

Sponsored results

Login to get rid of ads

 

0

Loading

Re: Trouble with focus() in javascript

Postby Rajmv » Sun Jul 17, 2011 5:01 pm

I have the following remarks about your test code;

- we don't really need the javascript code

- your <body> tag has an onload="window_onUnload()".
this window_onUnload() should be named window.onLoad().

and window_onUnload() does not call function f('something').

that's why it never shows up.

i corrected this small problem, and voila; window displays (when user enabled popups for the site) and focusses.

but since users will have to manually enable allowing popups, i recommend you switch to a lightbox; http://www.ajaxrain.com/tag?tag=lightbox

code that works on my machine here;
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" language="javascript">
<!--
    var win;
    function f(s) {
        website="http://machine/apache/jsfnu/mytest" + s + ".txt";
        if (!win)
            win = window.open( website , "thetest" , "toolbar=yes,resizable=yes");
        else
        {
            win.url = website;
        }
        win.navigate(website);
        win.focus();
    }

    function window_onload() {
      f('t');
    }
-->
</script>
</head>
<body bgcolor="#fdf8ed" text="black" language="Javascript" onload="window_onload()">

                <center>
                  <APPLET CODE="mytest.class" archive="mytest.jar,netscape.jar" NAME="myApplet"  MAYSCRIPT HEIGHT="1000" WIDTH="1000"> </APPLET>
                </center>
        </body>
</html>
Rajmv
 
Reputation: 0
Posts: 103
Joined: Thu Jul 14, 2011 8:22 am
Highscores: 0
Arcade winning challenges: 0
^ Back to Top