[Greasemonkey] preventing default handler

Nikolas Coukouma lists at atrus.org
Fri Jan 13 10:24:11 EST 2006


Karthik V wrote:
> Hi,
>
> I'd like to take control of a form submit action and run my own function.
> This should redirect the page elsewhere depending on the input. I used
>
> btn.addEventListener('click', myfunc, false)
>
> on the submit button. This executes myfunc properly but at the end, before
> it hits the line (in myfunc)
>
> window.location = newlink
>
> the page submits to the old url. I tried using event.preventDefault(), but
> this stops window.location also from working ... Please help me to fix this
> problem.
>   
The following works fine for me:

var b = document.getElementById("b");
function handle_submit(e) {
  window.location = "http://www.google.com/";
  e.preventDefault();
}

b.addEventListener('click', handle_submit, false);
> Also tell me what the 3rd parameter in addEventListener does (the boolean
> value).
>   
It selects the event phase. true for capture, false for bubble.

Nice explanation, with ASCII art:
http://www.quirksmode.org/js/events_order.html

Spec:
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow


More information about the Greasemonkey mailing list