admin管理员组

文章数量:1318335

I'm developing a Chrome extension that does something when a <td> tag is clicked in a web page.

Here's some sample code:

HTML:

<table>
    <tr>
        <td id="mytest"><a href=";>Foo Bar</a></td>
    </tr>
</table>

Javascript:

var myTd = document.getElementById("mytest");
myTd.addEventListener("click", function() {
    localStorage["foobar"] = 1;
});

When I click the link, the localStorage key is set, if I click it with the mouse middle button, it also sets the key (and opens the link in a new tab).

The problem is when I use right-click and "open link in a new tab". In this case the click event doesn't seem to be fired and therefore the localStorage key will not be set.

Am I missing something? Is there any way to make the right-click -> "open link in new tab" fire the click event?

Please note that I don't want to add the listener to the <a> node, because of some plications in the real HTML I'm working on.

I'm developing a Chrome extension that does something when a <td> tag is clicked in a web page.

Here's some sample code:

HTML:

<table>
    <tr>
        <td id="mytest"><a href="http://blablabla.">Foo Bar</a></td>
    </tr>
</table>

Javascript:

var myTd = document.getElementById("mytest");
myTd.addEventListener("click", function() {
    localStorage["foobar"] = 1;
});

When I click the link, the localStorage key is set, if I click it with the mouse middle button, it also sets the key (and opens the link in a new tab).

The problem is when I use right-click and "open link in a new tab". In this case the click event doesn't seem to be fired and therefore the localStorage key will not be set.

Am I missing something? Is there any way to make the right-click -> "open link in new tab" fire the click event?

Please note that I don't want to add the listener to the <a> node, because of some plications in the real HTML I'm working on.

Share Improve this question edited Dec 4, 2020 at 14:21 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 10, 2011 at 9:47 rogeriopvlrogeriopvl 54.2k8 gold badges57 silver badges58 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

nice question...

There is not a rightclick event on browser, chrome send the events mousedown, mouseup and contextmenu,

I found the following webpage quite useful, though I've not checked the rightbutton part, the general description of chain of events is quite faithful.

For a quick reference: http://unixpapa./js/mouse.html

Use mousedown event in place of click:

var myTd = document.getElementById("mytest");
myTd.addEventListener("mousedown", function() {
    localStorage["foobar"] = 1;
});

In this way even if the user chooses to "Open link in a new tab", it still works.

本文标签: javascriptChrome quotopen link in new tabquot not firing the click eventStack Overflow