Closed Bug 337966 Opened 18 years ago Closed 18 years ago

assignment to (default scope.)event in a javascript "with" block loses context

Categories

(Firefox :: General, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 159849

People

(Reporter: zzmozilla, Unassigned)

Details

Attachments

(1 file)

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3 --- code snippet --- window.onload = function() { with (document.getElementById('A2')) { onclick = function() {alert(this.nodeName)};} --- end code snippet --- The above registers for click events from everywhere (including outside 'A2'). Additionally, the 'alert' dialog displayed content will be "undefined". This holds true for other mouse events as well. A complete sample page is included below, in "Steps to Reproduce". Reproducible: Always Steps to Reproduce: -----load the following page----- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Firefox with.event issue</title> <style> div { background-color: silver; padding: 10px; margin: 10px;} div.alt { background-color: blue;} </style> <script type="text/javascript"> function EventAlert(e) { if (!e) e = window.event; var el=e.target||e.srcElement; alert( 'this '+this.nodeName+' is '+this.id+ ', '+ 'event '+el.nodeName+' is '+el.id);} function registration() { with (document.getElementById('A2')) { alert("registering onclick to "+nodeName+" "+id); onclick = EventAlert; //document.getElementById('A2').onclick = EventAlert; }} window.onload = registration; </script> </head> <body> the page loads in Firefox 1.5.0.3, and produces the message:<br /> "registering onlick to DIV A2"<br /> however, it appears to be handled at an undefined root.<br /> clicking div A1a produces the message:<br /> "this undefined is undefined, event DIV is A1a"<br /> <div id="A">A <div id="A1" class="alt">A1 <div id="A1a">A1a</div> </div> <div id="A2" class="alt">A2 <div id="A2a">A2a</div> </div> </div> </body> </html> Actual Results: --- when clicking A1a --- "this undefined is undefined, event DIV is A1a" --- when clicking A2a --- "this undefined is undefined, event DIV is A2a" Expected Results: --- when clicking A1a --- --- when clicking A2a --- "this DIV is A2, event DIV is A2a" Note that during the faulty registration (inside the "with" block), scope SEEMS to function properly. In the unmodified example above, "registering onlick to DIV A2" is displayed as expected. Also note that uncommenting the included remarked line... //document.getElementById('A2').onclick = EventAlert; ...is a workaround for developers targetting Firefox 1.5 and aware of this bug in this version. However I'm leaving the issue normal severity because, of course, developers are typically not aware of such issues when building cross-broswer solutions. Also note that the issue appears to be related to "with" and not the DOM status during window.onload. For example, window.onload can be replaced with an inline onclick="registration()" on another element. After clicking that element, the same bug is seen.
Note that, without uncommenting the remark, this page incorrectly responds to clicks everywhere, and produces an erroneous alert output.
*** This bug has been marked as a duplicate of 159849 ***
Status: UNCONFIRMED → RESOLVED
Closed: 18 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: