tags:

views:

73

answers:

5

Hi,

I've got a working a.click() function in jquery... But if I click an anchor, I open an new window... But how can I stop the browser from opening a new window itself??

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 the event??

+2  A: 

Add this to the end of your 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;
        });
});
Ender
+4  A: 

Try

e.preventDefault()

but here return false may also do it which is in the other answer. preventDefault can be used in more senarios and is a more generic "stop the default event action", see: http://api.jquery.com/event.preventDefault/

lasseespeholt
this worked (put I can't accept your answer... I have to wait -.-)
dododedodonl
+2  A: 

You could try

e.preventDefault();
MoDFoX
A: 

EDIT, I stand corrected .preventDefault is not a jQuery function.

jpluijmers
it only has to work in safari (it is a safari extention), and it works...
dododedodonl
It is not a jQuery function.
Felix Kling
Since it wasn't clarified in the question this still might be handy for other users stumbeling on the question don't you think?
jpluijmers
tested, did not work...
dododedodonl
+2  A: 
e.preventDefault();
Eton B.