How to disable or hide the option "Open in a new tab" for hyperlinks

In the hyperlinks in the right-click menu, how can I remove or hide the tab "Open in a new tab" and "Open in a new window"?

eg

<a href="#" onclick="asd">foo</a> 
+7
source share
3 answers

You don't know why you want to do this, but you can do this by moving the href attribute to data-href , then remove href and add a click handler. Onclick will read data-href and redirect.

Demo

 var links = document.getElementsByTagName("a"); for(var i=0; i<links.length; i++){ links[i].setAttribute("data-href", links[i].getAttribute("href")); links[i].removeAttribute("href"); links[i].onclick = function(){ window.location = this.getAttribute("data-href"); }; } 

The menu of the right mouse button displays:

enter image description here

+22
source

You can use javascript link instead of simple html tags. Just do href = "javascript: void (0)" and handle the click event to redirect the page. This will not remove the possibility of opening in another tab, but make sure that the page does not actually open when you try.

Instead of the HTML tag, you can instead use a different tag and point the cursor to it: pointer css property and jquery onclick so that it works as a link. This will completely remove the “open in another tab” option from the context menu.

+1
source

You can do this using the following code.

 <script language="javascript"> $("a").click(function(event) { if(event.button==2) { return false; } }); </script> 
0
source

All Articles