-3

I want to use the function blow to prevent the default action like "a href", but it just cant work in firefox.

function alertPopup(html) {
    event.preventDefault();
    // ...
}

and then I added the variable "event" into this function, as blow, but also failed; the firefox console displayed the error "ReferenceError: event is not defined".

function alertPopup(html) {

    function stepstop(event){
        console.log("tttttt");
        event.stopPropagation();
    };
    stepstop(event);
            // ...
}

<a href="#" onclick="alertPopup("hello");">XXXXX</a>

So how can I prevent default action in this function? Without using "return false"... Thank you!

Qimin Lu
  • 31
  • 1
  • 7
  • You're attempting to stop the global event object, which is not necessarily the same as the event being handled. Please add your code to the question where you call the `alertPopup()` function (presumably an event handler) – Rory McCrossan Mar 01 '16 at 13:06
  • where are you calling `stepstop`? – Rajaprabhu Aravindasamy Mar 01 '16 at 13:07
  • Reference error for event in firefox is thrown if the event handler is not supplied the event argument. Google doesnt throw this error and it exactly finds the event initiator.So while calling the stepstop function u r not passing the event object – Vijaykrish93 Mar 01 '16 at 13:10
  • Well you do not pass in event to your method. How is the method being called. – epascarello Mar 01 '16 at 13:17
  • In Firefox event is not a property of the global window object. An existing question on [how to access event object](http://stackoverflow.com/questions/5849370/get-the-event-object-in-an-event-handling-function-without-pass-the-event-object) may be able to answer your question. – traktor Mar 01 '16 at 13:19
  • It is important because event is not global like it is in some browsers! – epascarello Mar 01 '16 at 13:19
  • @epascarello so the 2rd method is also not working? – Qimin Lu Mar 01 '16 at 13:20
  • We need to see more code... How is stepstop being triggered... Show how the event is attached to the link. – epascarello Mar 01 '16 at 13:20
  • @epascarello sorry i misunderstood u, i forget to paste "stepstop()", while it's also not working – Qimin Lu Mar 01 '16 at 13:22
  • Where is the onclick code? That is what we need to see addEventLIstener("click"..) or onclick="xxxx". We need that code... – epascarello Mar 01 '16 at 13:26
  • @epascarello and i re-edit the question ...you can take a look, thank you – Qimin Lu Mar 01 '16 at 13:34

2 Answers2

1

Rather than use the onclick attribute on the anchor tag, try adding an event listener in the Javascript, so that you can preventDefault on the actual click event.

Using JQuery, it would look something like this:

$('a').on('click', function(e){
    e.preventDefault();
    ......
});
Luke P
  • 723
  • 7
  • 19
0

Pass in event

function alertPopup(event, html) {
    event.preventDefault();
    console.log(html);
}
<a href="#" onclick="alertPopup(event, 'hello');">XXXXX</a>

A better way would be to attach events with addEventListener() and not using inline events.

epascarello
  • 204,599
  • 20
  • 195
  • 236