Hi JoJo,
It seems you're doing a closure "by hand". You may want to look at
Function.bind() or Function.curry() in prototype documentation, since
they do it for you.
Your solution may become :
dynamicAnchor = new Element(
'a', {
href: 'javascript:void(0)'
}
).observe(
'click',
(function(parameter,event) {
window.status = parameter;
event.stop();
}).curry(localVariable)
);
Also note that I am 80% sure (please somebody confirm) that since your
parameter is a local variable, javascript will implicitly create a
closure for you.
In other words, this should work:
var localVariable= 'Hi!';
dynamicAnchor = new Element(
'a', {
href: 'javascript:void(0)'
}
).observe(
'click',
function(event) {
window.status = localVariable;
event.stop();
}
);
T.J.'s blog has many very well written articles that will explain you
why (http://blog.niftysnippets.org/search/label/closures).
Eric
On Oct 22, 8:47 pm, JoJo <[email protected]> wrote:
> Thanks T.J.
>
> The solution is:
>
> dynamicAnchor = new Element(
> 'a', {
> href: 'javascript:void(0)'
> }
> ).observe(
> 'click',
> function(parameter) {
> return function(event) {
> window.status = parameter;
> Event.stop(event);
> }
> }(localVariable)
> );
>
> On Oct 22, 4:39 am, "T.J. Crowder" <[email protected]> wrote:
>
> > Hi,
>
> > See the documentation for `Event.observe`[1] (which is what you're
> > calling indirectly from the `Element#observe` function), specifically
> > the "Preventing the Default Event Action and Bubbling" part, and the
> > `Event` object[2]: You don't return false to cancel the `click` event,
> > you use the `Event#preventDefault` function (which is a standard DOM
> > function[3] that Prototype ensures is present even on implementations
> > that lack it) or, more likely, `Event#stop`[4] which both prevents the
> > default action and stops the event bubbling.
>
> > [1]http://api.prototypejs.org/dom/event/observe/
> > [2]http://api.prototypejs.org/dom/event/
> > [3]http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event-prev...
> > [4]http://api.prototypejs.org/dom/event/stop/
>
> > HTH,
> > --
> > T.J. Crowder
> > Independent Software Engineer
> > tj / crowder software / com
> > www / crowder software / com
>
> > On Oct 22, 2:41 am, JoJo <[email protected]> wrote:
>
> > > I use anchors to call javascript functions. Usually, I do this:
>
> > > <a href="javascript:void(0)" onclick="someFunc(); return false;">
> > > call the function
> > > </a>
>
> > > This works fine in all browsers. Window.onbeforeunload is never
> > > triggered because the ONCLICK returns false, and thus the HREF if not
> > > executed. When HREF is not executed, the browser does not believe that
> > > the window in unloading. Now, I'm trying to create a dynamic anchor to
> > > accomplish the same thing. It looks nearly identical to the HTML
> > > anchor, but now it's triggering window.onbeforeunload in IE8. How do I
> > > not trigger it?
>
> > > dynamicAnchor = new Element(
> > > 'a', {
> > > href: 'javascript:void(0)',
> > > }
> > > ).observe(
> > > 'click',
> > > function(parameter) {
> > > return function() {
> > > window.status = parameter;
> > > return false;
> > > }
> > > }(localVariable)
> > > );
>
>
--
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en.