How To Capture Open Link In New Tab Or Window Using Jquery?
Solution 1:
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.)
Solution 2:
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.
Solution 3:
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");
}
Solution 4:
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.
Post a Comment for "How To Capture Open Link In New Tab Or Window Using Jquery?"