Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome: "open link in new tab" not firing the click event?

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.com">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 complications in the real HTML I'm working on.

like image 857
rogeriopvl Avatar asked May 10 '11 09:05

rogeriopvl


People also ask

How do I get Chrome to open a new tab when I click a link?

Simply press and hold the Ctrl key (Cmd on a Mac) and then click the link in your browser. The link will open in a new tab in the background.

Why on clicking the link is not working?

Browser Issues The lack of link-clicking might simply be a browser setting gone awry. If you can, try clicking on a link with a different browser to see if it works. If it does, then your browser settings are probably off and need to be reset. One way to do this quickly is to uninstall and reinstall the browser.

How do you make it so when you click a link it opens a new tab?

For this, you have to hold the CTRL button and then click on the left mouse button while pointing the cursor to the web address. If you click on a link in this manner, the website won't open in your current tab; instead, you'll see a new tab with your preferred web page.


2 Answers

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.com/js/mouse.html

like image 151
Eineki Avatar answered Sep 22 '22 04:09

Eineki


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.

like image 38
Marco Demaio Avatar answered Sep 22 '22 04:09

Marco Demaio