SimonNN
SimonNN

Reputation: 39

JavaScript click event

My browser (firefox) prevents any popup from loading, and loads links that open new windows in the current tab, unless I explicitly say I want the link to load on a new tab or window, with the appropriate shortcuts (for example, middle click on the link, or left click with ctrl pressed causes the link to open on a new tab, and shift + left click on a new window).

I would like to create a javascript function f() that runs some code (meant to create the link address) when the link is pressed, and then loads the link that has been created, without removing the user experience described above. Right now what I have is something like <a href="javascript:void(0)" onclick="f()"/>, but middle click doesn't work (it instead loads the url javascript:void(0)) and neither do the other features described above.

Do you have any idea as how to solve my problem ?

Thanks.

Upvotes: 1

Views: 12889

Answers (2)

LordZardeck
LordZardeck

Reputation: 8283

have you tried window.open('url')?

see: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml

Also, as far as I know, you can't control whether or not the browser opens in a new tab or new window. That is a browser setting that is different for every user.

You might also try removing the onclick, and using <a href="javascript:f()"></a>

EDIT

There seems to be issues with using middle click with opening new tabs instead of executing the javascript: middle click (new tabs) and javascript links

As that site says, you can instead create an id for the element and bind it through javascript.

**Taken from that link:

<a href="/non/ajax/display/page" id="thisLink">...</a>

And then in your JS, hook the link via it's ID to do the AJAX call. Remember that you need to stop the click event from bubbling up. Most frameworks have an event killer built in that you can call (just look at its Event class).

Here's the event handling and event-killer in jquery:

$("#thisLink").click(function(ev, ob) {
    alert("thisLink was clicked");
    ev.stopPropagation();
});

Without jQuery, it might look like this:

document.getElementById('thisLink').onclick = function(e)
{
     //do someting
     e.stopPropagation();
}

Upvotes: 5

Neil
Neil

Reputation: 55382

Other browsers may vary, but by default Firefox doesn't tell the web page that it has been middle-clicked (unless you set the hidden preference to enable the feature). You might be able to create a workaround based on the focus and/or mouseover events instead.

Upvotes: 0

Related Questions