When I use .click()
on an <a>
tag, the event only works when I click on the element. Otherwise, if the user does a Right Click > Open in new window or Open in new tab, it doesn't trigger the click()
event.
So, my question is...how do I trigger the click()
event when user does right click > open in new tab/window?
Here is the HTML:
<a href="url">Click Me</a>
Here is the Js:
$("a").click(function(){
alert('You clicked me!');
});
When I use .click()
on an <a>
tag, the event only works when I click on the element. Otherwise, if the user does a Right Click > Open in new window or Open in new tab, it doesn't trigger the click()
event.
So, my question is...how do I trigger the click()
event when user does right click > open in new tab/window?
Here is the HTML:
<a href="url">Click Me</a>
Here is the Js:
$("a").click(function(){
alert('You clicked me!');
});
Share
Improve this question
asked May 27, 2012 at 12:58
Control FreakControl Freak
13.2k30 gold badges99 silver badges150 bronze badges
4
-
ummm.. thats only a right click, not
open in new window/tab
. – Control Freak Commented May 27, 2012 at 13:00 -
1
I don't think it is possible. The only way I see is to catch mouse right button click and trigger
click
event manually. – VisioN Commented May 27, 2012 at 13:00 - I don't think it's possible. I think one of the reason is that you don't want the website to be able to track the external link you are visiting. – gigadot Commented May 27, 2012 at 13:04
- ok well i think i guess i will just have to use that.. – Control Freak Commented May 27, 2012 at 13:10
1 Answer
Reset to default 3You can try this code, but remember that changing the UI is not a good ideia:
var addEvent = (document.addEventListener) ?
function(target,event,fn){
if(target) return target.addEventListener(event,fn,false);
}:
function(target,event,fn){
if(target) return target.attachEvent(('on' + event),fn);
},
allLinks = document.links || document.getElementsByTagName('a');
for(var i=0;i<allLinks.length;i++)
addEvent(allLinks[i],'mouseup',function(e){
var e = e || event;
if(e.which===3){
alert('Open in new tab/window');
e.preventDefault();
return false;
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744180537a4561960.html
评论列表(0条)