How do I open a new tab with "ctrl + click" instead of redirecting the current tab in jQuery?

The table contains numerical values, each cell has its own href

.

If I applied hrefs like this:

nTd.click(function(e){
   $(this).css("font-weight","bold");
   e.stopPropagation();
   location.href = 'http://google.com';
});

      

Every click on the cell redirection window, I cannot open a new tab with "Ctrl + click".

If I added in TD

something like '<a href="http://google.com"> 123123 </a>'

, then sorting by numerical values ​​would break in lexicographic order.

+3


source to share


2 answers


Check to see if the CTRL key was pressed when the event was raised:

nTd.click(function(e){
   $(this).css("font-weight","bold");
   e.stopPropagation();
    if(e.originalEvent.ctrlKey){
        window.open("http://www.google.com", "_blank");
    } else {
        location.href = 'http://google.com';   
    }
});

      



JSFiddle

You won't see the page change in the script, but you will see the error it produces in the console.

+3


source


Leaving aside the original question, I would answer: No jquery required, you can just change your anchor tag to have a function target='_blank'

. So a complete example:

<a href="http://google.com" target="_blank"> 123123 </a>

      

Edit more detailed description and other thought . Alternatively, you can add this to your function:

window.open('url to open','window name','attribute1,attribute2')

      



So the actual example of a jam is there:

nTd.click(function(e){
   $(this).css("font-weight","bold");
   e.stopPropagation();
   window.open("http://www.google.com", "_blank");
});

      

Link to anchor label Javascript link to open a new window

0


source







All Articles