Reputation: 7924
I'm writing a website in which all content is stored in the JavaScript environment, so it should be possible to navigate between "pages" without additional HTTP requests.
I want to preserve the user's intent with respect to how links are opened, though. If a Mac user apple+clicks a link, for example, it should be opened in a new tab. But if the user wants to open a link in the current window, I want to avoid making a new HTTP request, and just use DOM manipulation to show the new content.
Here's what I tried (JSFiddle):
<a href="http://yahoo.com" id="mylink">Yahoo!</a>
document.getElementById("mylink").onclick = function() {
alert("clicked");
return false;
}
It behaves as desired for normal left clicks, and for context menu actions, but not for clicks with a modifier key. I could check whether certain modifier keys are held, but that seems fragile.
Twitter's website has behavior similar to what I want - when you click a username, it's normally an AJAX request, but if you click with a meta key held, you get a new tab.
I'd prefer a plain JavaScript solution without libraries, unless this requires a bunch of platform-specific logic.
I took a look at GitHub's code, which also has the behavior I'm after, and it does check for certain keys being held. So I'm accepting Chris's answer , since it seems unlikely that there's a better alternative.
$(document).on("click", ".js-directory-link", function (t) {
return 2 === t.which || t.metaKey || t.ctrlKey ? void 0 : ($(this).closest("tr").addClass("is-loading"), $(document.body).addClass("disables-context-loader"))
}
Upvotes: 37
Views: 39953
Reputation: 416
You can detect that using onblur as well
<html>
<head>
<script>
function newTabaction() {
document.getElementById("demo").innerHTML = "New tab opened!<br><br>refesh this page to recheck ";
}
window.onblur = newTabaction;
</script>
</head>
<body>
<div id="demo">
Open a new tab and then check this page
</div>
</body>
</html>
Upvotes: 1
Reputation: 50622
You can examine the ctrlKey
, shiftKey
, and metaKey
properties of the event object. If either is true, the key control, shift, or meta (Apple's command) key is being held and you should allow the default link action to proceed. Otherwise, you use preventDefault
to stop the link action and handle it with javascript instead.
Add target="_blank"
to your anchor markup, so the default link behavior is opening a new tab. Otherwise it will open on top of the current page (that may be desired).
Here's the javascript, either way:
document.getElementById("mylink").onclick = function(evnt) {
if (
evnt.ctrlKey ||
evnt.shiftKey ||
evnt.metaKey || // apple
(evnt.button && evnt.button == 1) // middle click, >IE9 + everyone else
){
return;
}
evnt.preventDefault();
alert("clicked");
return false;
}
Fiddle: http://jsfiddle.net/6byrt0wu/
Documentation
Event.ctrlKey
- https://developer.mozilla.org/en-US/docs/Web/API/event.ctrlKeyEvent.shiftKey
- https://developer.mozilla.org/en-US/docs/Web/API/event.shiftKeyEvent.metaKey
- https://developer.mozilla.org/en-US/docs/Web/API/event.metaKeya
tag - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/aUpvotes: 48