if a link is clicked - open a pop-up, but if somebody clicked it with open in new tab, a new tab should open

StackOverflow https://stackoverflow.com/questions/13603321

Question

is there any framework (jquery, or anything else) or any java script which can handle this problem:

  • if a link is clicked: open a popup
  • if somebody clicks with: middle mouse click or with right-click: open in new tab, it should open in a new tab.

I can't believe, there is no (complex) solution for it!
(and: would be nice if it's working on IE7+, FF, Safari, Chrome)

Was it helpful?

Solution

You simply need to bind a handler for the onclick event that will call window.open() to open the new window. I haven't tested in all browsers, but the ones I have tested in don't fire the onclick event when you use the middle (scrollwheel) or right mouse buttons.

HTML:

<a href="yourpage.html" class="popuplink">Click me!</a>

jQuery:

$('a.popuplink').on('click', function(e) {
    e.preventDefault(); // don't want to follow the link
    window.open(this.href, 'new_window', 'width=800,height=600').focus();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top