Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if user it trying to open a link in a new tab?

Tags:

javascript

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.

Update

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")) } 
like image 431
Daniel Lubarov Avatar asked Nov 20 '13 04:11

Daniel Lubarov


People also ask

Is it possible to detect if a user has opened a link in a new tab?

On every other page navigation in that same tab window.name is preserved. If you open a tab however, window.name is lost. At that point your script checks if the window.name is not blank, and because you're not on your first entry page, you've detected a new tab opened. Very neat!

How do I make sure links open in a new tab?

Use Mouse or Trackpad Only If you use a mouse, simply utilizing the middle mouse button to click on a link will immediately open it in a new browser tab! Holding down the Shift key while middle-clicking also helps you switch to the tab automatically. Trackpads on Windows laptops can also open links in new tabs.

How do you tell the browser to launch a link in a new window?

Open in a new window To open a link in a new browser window, hold the Shift on then click the link or right-click the link and select Open link in New Window.

Is there a way to open links in a new tab without switching to that tab?

1) select "open link in new background tab" from the context menu (second choice), 2) use middle-click on the link with your mouse; 3) use Ctrl+leftclick on the link with your mouse, 4) designate a mouse gesture to open a background tab when you perform it over a link.


1 Answers

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

  • MDN Events - https://developer.mozilla.org/en-US/docs/Web/API/Event
  • Event.ctrlKey - https://developer.mozilla.org/en-US/docs/Web/API/event.ctrlKey
  • Event.shiftKey - https://developer.mozilla.org/en-US/docs/Web/API/event.shiftKey
  • Event.metaKey - https://developer.mozilla.org/en-US/docs/Web/API/event.metaKey
  • a tag - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a
like image 86
Chris Baker Avatar answered Oct 05 '22 15:10

Chris Baker