[Javascript] Stopping default action

Bill Moseley moseley at hank.org
Thu Mar 30 14:52:51 CST 2006


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




More information about the Javascript mailing list