Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to navigate to a page with JavaScript *without* breaking "Open in a new tab"

Tags:

javascript

I'm using JavaScript to allow users of my site to click anywhere on a table row to visit the URL of the row's record. Setting location.href works fine except that when my users try to open the page in a new tab behind the current one, JavaScript breaks their browsers normal Shift+Click behavior. How can I change the page with JavaScript without this nasty side-effect?

like image 471
bloudermilk Avatar asked Dec 08 '25 06:12

bloudermilk


1 Answers

This is a browser behavior and you cannot replicate it in any way. Many workarounds may be detecting the Shift keypress and executing window.open, or inserting an <a/> element that surrounds all of each cells content, and dinamically changing the href on mousedonw, but all are ugly patches. This is one of many situations in which you face that javascript can do almost everything to manipulate the DOM, but not broswer actions that are commonly triggered when interacting with the DOM.

like image 176
Áxel Costas Pena Avatar answered Dec 09 '25 19:12

Áxel Costas Pena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!