JQuery - binding

I have a working a.click () function in jquery ... But if I click on the anchor, I will open a new window ... But how can I stop the browser from opening the newest window ??

Example:

    $('a').each(function() {
            $(this).click(function(e) {
                if($(this).attr('target') == '_popup') {
                    //here is code to open window (already exists)

                    //try to stop propagation, but doesn't work...
                    e.stopPropagation();
                }

                hideNSL();
            });
    });

so how do i stop an event?

+5
source share
5 answers

Try

e.preventDefault()

but here return falsecan also do it, which is in a different answer. preventDefaultcan be used in more senarii and is more general to "terminate the default event", see http://api.jquery.com/event.preventDefault/

+12
source

Add this to the end of the click event:

return false;

So for example:

$('a').each(function() {
        $(this).click(function(e) {
            var toReturn = true;
            if($(this).attr('target') == '_popup') {
                //here is code to open window (already exists)

                toReturn = false;
            }

            hideNSL();
            return toReturn;
        });
});
+4
source

You can try

e.preventDefault();
+3
source
e.preventDefault();
+3
source

EDIT, I am standing fixed .preventDefault is not a jQuery function.

-1
source

All Articles