Prev: iframes? something better?
Next: ISO 8601 date format
From: laredotornado on 23 Apr 2010 09:18 Hi, I'm trying to do something simple that is blowing my mind right now. I'm on Firefox on Mac 10.6.3. I have this function ... <script type="text/javascript"> function delete(ruleId) { if (confirm("Are you sure you want to delete this rule?")) { location = '/sweeps/delete?id=' + ruleId; } // if } // ruleId </script> and then I have this link ... <a href="javascript:var ret = delete(2);">Delete</a> Clicking on the link does nothing (function isn't invoked and there are no JS errors in the console). If I take out the "var ret =", the browser attempts to load the Javascript in the address bar and the output is "true". What am I doing wrong? Thanks, - Dave
From: Sean Kinsey on 23 Apr 2010 09:28 On Apr 23, 3:18 pm, laredotornado <laredotorn...(a)zipmail.com> wrote: > Hi, > > I'm trying to do something simple that is blowing my mind right now. > I'm on Firefox on Mac 10.6.3. I have this function ... > > <script type="text/javascript"> > function delete(ruleId) { > if (confirm("Are you sure you want to delete this rule?")) { > location = '/sweeps/delete?id=' + ruleId; > } // if > } // ruleId > </script> > > and then I have this link ... > > <a href="javascript:var ret = delete(2);">Delete</a> > > Clicking on the link does nothing (function isn't invoked and there > are no JS errors in the console). If I take out the "var ret =", the > browser attempts to load the Javascript in the address bar and the > output is "true". What am I doing wrong? > > Thanks, - Dave use "javascript:void(delete(2))" to fix your issue. BUT.. NEVER EVER EVER expose methods with side effects (create, modify, delete) using GET (what you are doing). There has been stories about web spiders that have caused havoc because of this, and unexpected behavior in applications due to some browser preloading url's that it 'think' the user might navigate to.
From: Thomas 'PointedEars' Lahn on 23 Apr 2010 09:37 laredotornado wrote: > <a href="javascript:var ret = delete(2);">Delete</a> > > Clicking on the link does nothing (function isn't invoked and there > are no JS errors in the console). If I take out the "var ret =", the > browser attempts to load the Javascript in the address bar and the > output is "true". What am I doing wrong? `delete' is an operator, a reserved word, a keyword (ES5, 7.6.1.1). It can never be the identifier of a function declaration (ES5, section 13; you should have gotten a syntax error before). Since no object, including the global object, has a `2' property to begin with, evaluation of the /UnaryExpression/ does not result in a Reference, so the result is `true' (ES5, section 11.4.1, step 2). And read the FAQ why you should not use `javascript:'. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Thomas 'PointedEars' Lahn on 23 Apr 2010 10:02 Thomas 'PointedEars' Lahn wrote: > laredotornado wrote: >> <a href="javascript:var ret = delete(2);">Delete</a> >> >> Clicking on the link does nothing (function isn't invoked and there >> are no JS errors in the console). If I take out the "var ret =", the >> browser attempts to load the Javascript in the address bar and the >> output is "true". What am I doing wrong? > > `delete' is an operator, a reserved word, a keyword (ES5, 7.6.1.1). It > can never be the identifier of a function declaration (ES5, section 13; > you > should have gotten a syntax error before). Since no object, including the > global object, has a `2' property to begin with, evaluation of the > /UnaryExpression/ does not result in a Reference, so the result is `true' > (ES5, section 11.4.1, step 2). Sorry, the explanation is not quite correct. The reason is that `2' cannot be produced by /MemberExpression/ or /Identifier/, which would result in a Reference value. Instead, it can only be produced by /DecimalIntegerLiteral/, through /DecimalLiteral/, through /NumericLiteral/, through /Literal/, and the result of that is not a Reference value. var o = {2: "foo"}; with (o) { delete 2; } /* "foo" */ console.log(o[2]); with (o) { delete o[2]; } /* undefined */ console.log(o[2]); o = {a: "foo"}; with (o) { delete a; } /* undefined */ console.log(o.a); PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Thomas 'PointedEars' Lahn on 23 Apr 2010 12:04
Sean Kinsey wrote: > laredotornado wrote: >> I'm trying to do something simple that is blowing my mind right now. >> I'm on Firefox on Mac 10.6.3. I have this function ... >> >> <script type="text/javascript"> >> function delete(ruleId) { >> if (confirm("Are you sure you want to delete this rule?")) { >> location = '/sweeps/delete?id=' + ruleId; >> } // if >> } // ruleId >> </script> >> >> and then I have this link ... >> >> <a href="javascript:var ret = delete(2);">Delete</a> >> >> Clicking on the link does nothing (function isn't invoked and there >> are no JS errors in the console). If I take out the "var ret =", the >> browser attempts to load the Javascript in the address bar and the >> output is "true". What am I doing wrong? > > use "javascript:void(delete(2))" to fix your issue. No, `delete' would be still parsed as the operator. Interestingly enough, it turns out you can declare a function with identifier `delete' in Mozilla.org JavaScript 1.8.2 (no syntax error), but you cannot access it. That you can declare it, is a bug. > BUT.. NEVER EVER EVER expose methods with side effects (create, > modify, delete) using GET (what you are doing). True. > There has been stories about web spiders that have caused havoc > because of this, Those spiders should then be blocked as they would be FUBAR if they existed. > and unexpected behavior in applications due to some browser preloading > url's that it 'think' the user might navigate to. If that applied here, one could not ever use the `location' property in Web applications. You are confusing this with URI-type element attributes, and it is doubtful whether those browsers should not be considered buggy as well in that case. Stop spreading FUD. PointedEars -- Danny Goodman's books are out of date and teach practices that are positively harmful for cross-browser scripting. -- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004) |