[whatwg] [html5] scope chain for event handlers specified via content attributes

David Flanagan dflanagan at mozilla.com
Thu Sep 8 17:23:09 PDT 2011


I've always assumed that if I do e.setAttribute("onclick", "alert(x)"), 
the resulting event handler function is (or works like) this:

function(event) {
     with(event.target.ownerDocument) {
         with(event.target.form || {}) {
             with(event.target) {
                 alert(x);
             }
         }
     }
}

That is, I'd expect the scope chain to be created dynamically for each 
invocation of the function.

But §7.1.6.1 says that the scope chain should be initialized statically 
when the content attribute string is converted to a function.  I'd like 
to check that that is intentional, since it causes counter-intiuitive 
(to me) behavior if an element moves between forms or moves between 
documents after the event handler attribute is set.

I've put some test code here:  http://pastebin.mozilla.org/1326758

My results:  Firefox and Safari create the scope chain statically: when 
an element moves between forms, the scope chain remains the same.  
Chrome's scope chain is dynamic and resolves identifiers against the 
element's new form.  Chrome's behavior seems more sensible to me. Is it 
correct?

(When an element moves from one document to another (via adoptNode()) 
firefox uses dynamic scope (perhaps because it is re-creating the 
function?).  In Chrome and Safari, the event handler stops working when 
the element is moved from one document to another, so the test doesn't 
succeed there.)

     David


More information about the whatwg mailing list