Is it possible to capture the right click open in new window/tab or mouse wheel open in new window/tab event using jQuery?
UPDATE 1
Here is why I need it. I have codeigniter application which uses pagination class. I use this class to display a grid. The pagination links have been bind with a method that uses AJAX to load the next page in a container div. Now some one can right click and open the next page in new tab/window which I don't want. IMHO, the only way to handle this is to some how trap the (right click or mouse wheel button click) open in new window/tab event.
UPDATE 2
I just realised all my AJAX requests are being served by one CI controller which actually acts as a proxy to other classes/libs. In this controller I can look at the request and if it isn't an AJAX request I can redirect the user to another page.

- 5,753
- 72
- 57
- 129

- 5,038
- 7
- 39
- 51
-
1I'm pretty sure the answer is "no", but good question. – nnnnnn Oct 21 '11 at 04:15
-
1Even I am not sure it is not possible. But I want to prevent it some how. I will add details as why I need it and some one can provide another solution – Kumar Oct 21 '11 at 04:19
-
ARGHHH!!! The comment above should be read as "Even I am sure it is not possible." – Kumar Oct 21 '11 at 04:29
4 Answers
A workaround solution is to replace all applicable <a>
elements with buttons, where (obviously) the buttons would call JavaScript that does the appropriate navigation.
If you're really keen you can apply CSS to make the buttons look like <a>
elements, though I don't recommend it because it confuses users who might try to treat them as standard links and right- or middle-click them.
(You could even get it to work for users that don't have JavaScript enabled by, e.g., making each button a submit button in its own little form.)

- 147,572
- 30
- 200
- 241
-
1I really do not want to tinker with pagination lib. The gird is just an example. A solution that I have found is to look at the request on server side and if it is not an AJAX request redirect it to some other page. – Kumar Oct 21 '11 at 06:09
At the very least you can catch a right-click, using .mousedown()
(or, presumably, mouseup()
). See this StackOverflow answer about right clicks for more. And by catching it, you should be able to do a standard event.preventDefault()
and then do as you like from there. That may be overkill, however, as it could prevent the user from doing other things you want to allow them to do.

- 1
- 1

- 3,125
- 1
- 23
- 35
-
Yes you are right when you say that is overkill. Controlling context menu is not a right option. – Kumar Oct 21 '11 at 04:25
-
I thought that might be the case (and I generally tend to agree about the context menu; that's one of my major bugbears with Flash). I can't think of any other way you might be able to catch it, though. Perfectly valid philosophical points aside, I'm wondering if it actually matters in this case, given your explanation of the issue. If you don't want them opening in a new tab, what *do* you want them to be able to do to the element in question that they'd get with a right-click? It wouldn't be optimal, but it would at least get the job done. – Chris Krycho Oct 21 '11 at 04:30
-
Point noted. I will think over it. But there are many other similar scenarios. – Kumar Oct 21 '11 at 04:36
-
Definitely. I'm also hoping someone can come up with another, better answer to your question, as it would be a great thing to have in the mental toolbox. – Chris Krycho Oct 21 '11 at 04:37
I almost fixed a similar issue now for a page which I am working on. My fix was to do some changes in the page if that has been opened in a new window....
Assume that you open a page "B" from page "A" in a new window.
If you want to check the page "B" is opened in a new window from page "A", then follow the below steps..
If (document.referrer == "A" && window.history.length > 1) {
alert("I am page 'B' and opened from page 'A' in a new window");
}
If you don't want people to access link the usual way or fallback when the JS is disabled, then it shouldn't be a link. Just use any element you like (span, div, button, whatever you like) and style it like a link. Then bind the action using JS. Or you can use a link with href="#"
or href="javascript: void(0)"
. That way if users right click it and choose to open in a new window, then they will end up in the same page they were before.

- 4,277
- 1
- 20
- 27