Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable or hide the Open In New Tab option for hyperlinks [closed]

Tags:

javascript

On hyperlinks in the right click menu, how can I remove or hide the Open In New Tab and Open In New Window options?

for example

<a href="#" onclick="asd">foo</a>
like image 377
Ashish Arora Avatar asked Jun 18 '13 12:06

Ashish Arora


People also ask

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

Some links are coded to open in the current tab while others open in a new tab. To take control of this behavior, press Ctrl when you click a link to stay on your current page while opening the link in a new tab in the background.

Why do links open in new tab?

Opening an external link in a new tab allows users to explore the other site as much as they want without having to hit the back button again and again to go back to your site. All they need to do is click the tab your site is on. There's no excessive back-button pressing or long wait times.

How do you make a hyperlink open in a new tab?

You can make a HTML link open in a new tab by adding the target=”_blank” attribute. You should insert this after the link address.

How do I stop a new tab from opening when I click a link edge?

Click the hamburger menu icon in the top-right corner. Select “Safe Search” from the menu. Scroll down to the “Results” section and uncheck both “Open links from search results in a new tab or window” and “Open links from news results in a new tab or window.”


2 Answers

You can use javascript link instead of plain html ones. Just do href="javascript:void(0)" and handle the click event to redirect the page. This won't remove the option of opening in another tab but will make sure the page doesn't actually open up when tried.

Also instead of an HTML tag, you can instead use another tag like and give it a cursor:pointer css property and jquery onclick to make it work like a link. This will completely remove the option "open in another tab" from context menu.

like image 182
tornados Avatar answered Nov 15 '22 13:11

tornados


Not sure why you'd want to do this but it can be done by moving the href to a data-href attribute, then remove the href and add a click handler. The onclick will read the 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 right click menu shows:

enter image description here

like image 44
MrCode Avatar answered Nov 15 '22 14:11

MrCode