[Javascript] Stopping default action
Mike Dougherty
mdougherty at pbp.com
Thu Mar 30 17:48:32 CST 2006
I just had a reason to research this myself...
http://www.gerd-riesselmann.net/archives/2005/03/a-firefox-javascript-bug
Read the author's thoughts describing your problem, then see the first comment for the (obvious?)
solution. :)
On Thu, 30 Mar 2006 12:52:51 -0800
Bill Moseley <moseley at hank.org> wrote:
> I'm moving an inline onclick to a registered event. The event
> triggers an AJAX request.
>
> The problem is that now the AJAX request AND the normal requests are
> made -- the default action still happens.
>
>
> This is what I had before on a <a href="/path/to/recent"> link:
>
> onclick="new Ajax.Updater('my_div', '/path/to/recent', { onComplete: Behaviour.apply , method:
>'get', requestHeaders: ['X-Ajax-Updater', 'ajax_table'] } ); return false;"
>
> Which returns false to stop the default behavior (follow the link).
> That worked fine -- "my_div" is updated by the AJAX call.
>
>
> So, the new setup:
>
> <a href="/path/to/recent" id="updater9">recent</a>
>
> Then in <script> at the end of the page:
>
> updater( 'updater9', 'my_div', '/path/to/recent' );
>
> And my javascript:
>
>
> function updater( linkID, target, url ) {
>
> var my_update = function(e) {
> new Ajax.Updater( target, url,
> {
> onComplete: Behaviour.apply, /* not pretty */
> method: 'get',
> requestHeaders: ['X-Ajax-Updater', 'ajax_table']
> });
> Event.stop(e); // Hey why won't the default behavior stop!!
> return false;
> }
>
>
> Event.observe( linkID, 'click', my_update, 'false' );
> }
>
>Firefox (and the request headers from the browsers) show that the
> ajax request is made, but also the normal href is followed.
> Shouldn't returning false stop the default behavior?
>
>
> Next question: Is there a smarter way to do the "updater()"
> function? I'm using a closure to save the "target" (the <div> that
> Ajax.Updater updates) for when the event finally happens.
>
>
> Thanks,
>
>
> --
> Bill Moseley
> moseley at hank.org
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
>
>
> __________________________________________________________
> This message was scanned by ATX
> 3:53:19 PM ET - 3/30/2006
More information about the Javascript
mailing list