Closed Bug 289623 Opened 20 years ago Closed 19 years ago

Unable to set focus to an object with javascript.

Categories

(Firefox :: General, defect)

x86
Windows XP
defect
Not set
major

Tracking

()

RESOLVED DUPLICATE of bug 223518

People

(Reporter: jhilton, Assigned: bugzilla)

References

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6) Gecko/20050317 Firefox/1.0.2
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6) Gecko/20050317 Firefox/1.0.2

Per the w3c standards every html element should have a focus attribute. When
trying to set focus to a div tag with javascript I get a javascript error saying
that focus is not a function for that item.

Error: document.getElementById("obj0").focus is not a function
Source File: javascript:document.getElementById("obj0").focus();
Line: 1

Reproducible: Always

Steps to Reproduce:
1.create a div with an object
2.using javascript attempt to set focus to that object
3.
Actual Results:  
I got a javascript error:

Error: document.getElementById("obj0").focus is not a function
Source File: javascript:document.getElementById("obj0").focus();
Line: 1

Expected Results:  
Set focus to that object.
Blocks: 289624
> Per the w3c standards every html element should have a focus attribute.
Could you give a link to that?

Also you should test with the latest nightly trunk build, which can be
downloaded here:
http://ftp.uni-erlangen.de/pub/mozilla.org/firefox/nightly/latest-trunk/

It could be that <object> tags can be focused with javascript instantly, I'm not
sure. A minimal testcase of the problem you're seeing would be nice (can be
attached by the link "Create a New Attachment")

Other ways in the current trunk build to make that code work is using a tabindex
attribute ( http://www.mozilla.org/access/keyboard/tabindex.html ) or using as
style overflow:auto/overflow:scroll;
That's not a link a w3c standard.
I would expect it to find it here, but I can't:
http://www.w3.org/TR/REC-DOM-Level-1/level-one-html.html#ID-011100101
Anyway, the link I described before (
http://www.mozilla.org/access/keyboard/tabindex.html ), is the way Mozilla has
implemented it now.
Focus is not generally supported by all elements. Links, form controls, and
windows can receive focus. Focus on links and form controls is part of the DOM 1
specification, while window.focus() has been supported since Netscape 3 and IE
4, but is a convention, not specification. To quote Ecma-262, however, ". . .
the host environment provides a means to attach scripting code
to events such as change of focus, page and image loading, unloading, error and
abort, selection, form
submission, and mouse actions."

On Xandros 3.0.2 using Firefox 1.0.4 neither window.focus(), nor window.blur()
functions. This is regardless of the settings in Preferences. On the same
installation, Konqueror performed as expected. Additionally, the same code
functions in Mozilla 1.6 on Xandros 2. Mozilla 1.7.8, IE 6, and Opera 7.54 on
Windows XP Pro SP2 as well as IE 6, Mozilla, various versions, Netscape, various
versions, and Opera 6 on Win 98. Here is a test page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
</style>
<script type="text/javascript">
var txt='<a href="#" onclick="opener.focus();return false;">blur this<\/a>';
</script>
</head>
<body>
<a href="#"
onclick="win=window.open('','','height=200,width=200,top=300,left=200');win.document.write(txt);win.document.close();return
false;">build win</a><br />
<a href="#" onclick="win.focus();return false;">focus on win</a><br />
<a href="#" onclick="win.close();return false;">close win</a>
</body>
</html>
(In reply to comment #0)
> Per the w3c standards every html element should have a focus attribute

You are probably confusing HTML events and UI events.

See "1.6.1. User Interface event types" of DOM2 specifications for UI events. 
http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-eventgroupings-uievents-h3

Interface UIEvent is introduced in DOM Level 2.
DOMFocusIn/DOMFocusOut/DOMActivate are defined here.
This section says next too.
> Note: To create an instance of the UIEvent interface, use the feature string
> "UIEvents" as the value of the input parameter used with the createEvent
> method of the DocumentEvent interface.

document.getElementById("ID of an HTML element").focus/blur is not for this
event type.
I don't know whether current Mozilla's implementation on window.focus etc.
utilizes these new events or not. 

See "1.6.5. HTML event types" of DOM2 specifications for HTML events.
http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-eventgroupings-htmlevents-h3

focus/blur are defined in this section and valid for the following elements:
 LABEL, INPUT, SELECT, TEXTAREA, and BUTTON.
document.getElementById("ID of an HTML element").focus/blur is valid only when
these HTML elements.

=> INVALID
I tried out the latest development release of the Mozile WYSIWYG editor
(http://mozile.mozdev.org/0.7/demos/basic.html) with DeerPark Alfa 2. It works
fine with FF 1.0; DeerPark does not work (Alfa 1/2, Mac/Win). 
Mozile 0.7 works by attaching an XBL widget to the html tags containing what the
site manager wants editable. This is mostly <div>s or <p>s. The initialization
code uses a <handler> for the focus event, showing a XUL toolbar when an
editable element has focus (there are <handler>s for blur, keypress, keyup,
selection etc too, but focus is the first being called). DeerPark does not call
focus/blur handlers, while FF does.
Is this problema related to the one discussed in this bug? It kind of look the
same, but reversed...
If it is, is this a voluntary change to the FF 1.1 event handling? 
Even if W3C does not mandate all html elements to respond to focus() calls, or
handling onfocus handlers, the whole idea of Mozillas -moz-user-focus style and
-moz-user-modify: read-write *does* count on html elements being focus-able, right?

Test case:
----------
<!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
	<head>
		<title>focus...</title>
		<style>
		#testDIV{
			-moz-user-focus: normal;
		}
		</style>
	</head>
	<body>
		Text outside the div<br/>
		<div id="testDIV" 
onfocus="alert('focus')" onblur="alert('blur')">
			Text inside the div. <br/>
			alert()s should pop in DeerPark too when clicking here!
		</div>
		<br/>
		Text outside the div
	</body>
</html>
-------------------------

Would really appreciate some help on this...

:-(
-moz-user-focus doesn't work anymore for html elements, see bug 256003, you
should tabindex instead.

*** This bug has been marked as a duplicate of 223518 ***
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.