So I have my website, a website without reload between site switches. For SEO reasons, my links look like this:
JavaScript
x
9
1
<a href="/site2" id="viewmore">View more</a>
2
<script>
3
// code is strongly simplified, for more clarity
4
document.getElementById("viewmore").addEventListener("click",function(e) {
5
e.preventDefault();
6
myHandler.goTo(e.currentTarget.href);
7
});
8
</script>
9
myHandler is the site handler in this case.
If I click the hotkey for opening a link in a new tab (on Mac it is CMD+Click), it does not work, as the event is prevented.
How to check if the link should open in a new tab or not? (Check if the hotkey is pressed or not)
Advertisement
Answer
Not sure I understood it, but try this
JavaScript
1
6
1
document.getElementById("viewmore").addEventListener("click", (e) => {
2
// ctrlKey / altKey / shiftKey
3
if (e.ctrlKey) myHandler.goTo(e.currentTarget.href);
4
else e.preventDefault();
5
});
6