?

Log in

No account? Create an account
Safari weirdness - brad's life — LiveJournal [entries|archive|friends|userinfo]
Brad Fitzpatrick

[ website | bradfitz.com ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Safari weirdness [Apr. 6th, 2005|06:10 pm]
Brad Fitzpatrick
Had to trace down this JavaScript bug in Safari:

http://www.danga.com/demo/safari-weird-1.html

Fortunately I understand why now (see the link for details), and it's fixed in Tiger.

Still fucked in the latest Konqueror 3.3.2 (in KDE), though.
LinkReply

Comments:
[User Picture]From: troworld
2005-04-07 04:34 am (UTC)
Latest Konqueror is 3.4.0. It still doesn't work there, though. :/
(Reply) (Thread)
[User Picture]From: fxl
2005-04-07 07:38 am (UTC)
I really do not think this is a bug.

function createHandler (num) {
    return function (e) {
        e = e || window.event;
        alert(num);
    };
}

function setup () {
  document.getElementById('but').onclick = createHandler(123);
}


When you do onclick = createHandler(123); onclick gets the return value of createHandler which is a function. The alert in that function is no longer in the context of createHandler and therefore should not have access to the variable num as it no longer exists in that scope.

In order to do the same thing, I would write it as:
function handler (num, e) {
  e = e || window.event;
  alert(num);
}
function setup () {
  // Add code to the onclick handler sending a value to the handler
  // function
  document.getElementById('but').onclick = "handler(123, event);";
}


Anyhow, just my two cents. I work for a company that places js probes that are placed on other companies websites that insert themselves in to various page events (while maintaining the original events), in order to do page personalization, customer reactivation, and customer tracking. I have to deal with these issues every day, even though I am really a back end code/tools guy.

By the way, I was impressed by the drawing tool.
(Reply) (Thread)
[User Picture]From: brad
2005-04-07 04:24 pm (UTC)
I disagree. The anonymous function that createHandler returns should capture the current scope, which should include in the parameter list.

My quick skimming of the ECMA spec makes me think so even more:

Section 10.1.3: says how function's formal parameters get added to the variable object. (so they're not special or outside the scope or anything)

Section 13: says how the definition of an anonymous function gets the scope chain of the running execution context as its Scope.

Gotta run to work now, but I'd be happy to be shot down on this. I'm still in the process of learning JavaScript.
(Reply) (Parent) (Thread)
[User Picture]From: mart
2005-04-07 06:38 pm (UTC)

My understanding agrees with you.

(Reply) (Parent) (Thread)
[User Picture]From: perlmonger
2005-04-07 06:26 pm (UTC)
It is a bug (unless you think that support for closures is a bug ;)

Having said that, it had never even occured to me to check if javascript supported closures; this is frustrating: finding out it does, and finding out I can't use them safely in the same LJ post...
(Reply) (Parent) (Thread)
[User Picture]From: brad
2005-04-07 06:33 pm (UTC)
They work. This is the only caveat I've found, and it's easy to work around: just save the function arguments into another function-local variable.
(Reply) (Parent) (Thread)
[User Picture]From: perlmonger
2005-04-07 06:58 pm (UTC)
Cool - without (yet) testing, I'd feared that access to all enclosed variables was fscked in KHTML.

Thanks for the heads up :)
(Reply) (Parent) (Thread)
(Deleted comment)
[User Picture]From: brad
2005-04-07 06:31 pm (UTC)
They are supposed to be closures. All other function-local variables are captured, and IE/Mozilla also capture the function's arguments.
(Reply) (Parent) (Thread)