Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with focus() in javascript

Status
Not open for further replies.

axsagb

Programmer
May 23, 2011
1
US
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:
var win;

function f(s) {
   	website="[URL unfurl="true"]http://machine/apache/jsfnu/mytest"[/URL] + 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:
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:
<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>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top