jQuery/JavaScript - Allow CTRL + Click to open link in new tab

nsilva

Is there a way in jQuery or JavaScript to enable the user to CTRL + Click a link to open a link in a new tab like <a href="">example</a> element?

Here is an example of what I am talking about:-

jQuery('#some-link').bind('click', function() {
   window.location = 'http://someurl.com';
});

If you hold CTRL and click the 'some-link' element, it just opens the page within the current page when using the above code, any ideas on how do this?

Bruno Calza

Check to see if the Ctrl key is pressed, and open the link using window.open. The link may not open in a new tab though. See the discussion here.

jQuery('#some-link').bind('click', function(e) {
   e.preventDefault(); 
   if (e.ctrlKey){
     window.open('http://someurl.com','_blank')
   }
});

See JSFiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

AngularJS $state open link in new tab using ctrl + click

Bind click event to open a link in new tab

Right Click and save link to open in new tab

Material ui card open in new tab on Ctrl+click

Why does CTRL-click not open some links in a new tab?

'Open in new tab' click on link not open href value

Open tab on click on link

Open link in a new tab

How to click a link and open it in a new tab geckodriver selenium?

Custom right click for anchor tag to open link in new tab

Window does not open new window or tab on external url link click

Google Chrome: Open a link in new tab and switch to it in one click

phpBB open link in new tab

Open link in new tab or window

Vaadin open link in new tab

Button to open link in new tab

pandoc: Open link in new tab

If statement to open a link in new tab

Open to new window or tab on click

"Ctrl + click to follow link" in VSCode terminal does not jump to source file but opens new browser tab

How to open a link embeded in a webelement with in the main tab, in a new tab of the same window using Control + Click of Selenium Webdriver

After clicking a link, open new URL and automatically *click* on tab link inside that page

How to right click on a link and open the link in a new tab using Selenium through Java

Django - Click on link won't redirect me to the page unless i right click and open in new tab

Rails: Open link in new tab (with 'link_to')

How to find out why Angular app running on IIS does not allow 'Open link in new tab' for a route / url?

how to open text in webpage as link by right click "Go to web address" in new tab

firefox: remove "Open Link in New Tab" from the right-click menu

How do you delay an click event on a link (with a _blank target) and still have the new tab open?