views:

195

answers:

2

I've tried to implement a cursor focus on an input area and IE has given my a lot of headaches. I've tried things along the line of (changing the timeout too, to large numbers):

setTimeout(function() { document.getElementById('myInput').focus(); }, 10);

and it won't work. I've noticed that I have an applet on the page as well, and when I remove the applet, the above works. Also if I put an alert before the focus() trigger, it will work.

The applet doesn't do anything in particular (it actually retrieves the user's MAC address) and it makes one call to an external JS function at the end to send the MAC address to the DOM. I've tried putting the focus (timeout and all) at the end of that JS function but that won't work either.

What could be wrong? Things work well in Firefox, just not in IE.

Current setup:

//This is called from within the applet using a window.call
function everythingDone()
{
$("#someinput").focus();
//setTimeout(function() { document.getElementById('someInput').focus(); }, 1000);
};

$(function() 
{
    var applet = "<object classid='clsid:CAFEEFAC-0014-0002-0000-ABCDEFFEDCBA' width='0' height='0'><param name='code' value='someapplet.class' /><param name='archive' value='/someapplet.jar' /></object>";
    $("#appletarea").html(applet);
    //setTimeout(function() { document.getElementById('someInput').focus(); }, 1000);
});
+1  A: 

This is an infamous bug in Sun Java plugin for IE. You can find a full discussion at http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4984794. Unfortunatly the bug is closed as not reproducible! Among the workarounds proposed, the following worked great for me:

Crookster

If anyone is interested, I believe I found a workaround for this.

In your applet start() method, add the following:

JPanel panel = new JPanel(); this.add(panel); panel.addFocusListener(new FocusAdapter() { public void focusGained(FocusEvent e) { JSObject win = (JSObject) JSObject.getWindow(this); win.eval("onAppletLoad();"); } }); panel.requestFocusInWindow();

Then, within your HTML code, add the following function:

function onAppletLoad() { myField.focus(); //where myField is the field to get focus!! }

You'll have to import the following in the applet

import netscape.javascript.*; // JSObject class, used to get the HTML page

and add plugin.jar to your classpath. You'll find plugin.jar within your JRE /jre/lib directory (1.4.x+)

Although the applet has a JPanel, just set the size to 0,0 and you won't see it.

Hope this helps

Shawn

A: 

I think a simpler workaround is to use the initial_focus parameter for the applet/object/embed tag.

<param name="initial_focus" value="false">
JB Hurteaux