3

I have created a search engine using Angular and Bootstrap. I have created different results tab as shown in image Tabs . Right clicking on tab does not show the option of a link as we get on any link. link on right click Currently I am using <li> tag and bootstrap to create the tabs of different result section. Here is the code for that

<ul type="none" id="search-options">
        <li [class.active_view]="Display('all')" (click)="docClick()">All</li>
        <li [class.active_view]="Display('images')" (click)="imageClick()">Images</li>
        <li [class.active_view]="Display('videos')" (click)="videoClick()">Videos</li>
        <li [class.active_view]="Display('news')" (click)="newsClick()">News</li>
        <li class="dropdown">
          <a href="#" id="settings" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
            Settings
          </a>
          <ul class="dropdown-menu" aria-labelledby="settings" id="setting-dropdown">
            <li routerLink="/preferences">Search settings</li>
            <li data-toggle="modal" data-target="#customization">Customization</li>
          </ul>
        </li>
        <li class="dropdown">
          <a href="#" id="tools" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
            Tools
          </a>
          <ul class="dropdown-menu" aria-labelledby="tools" id="tool-dropdown">
            <li (click)="filterByContext()">Context Ranking</li>
            <li (click)="filterByDate()">Sort by Date</li>
            <li data-toggle="modal" data-target="#myModal">Advanced Search</li>
          </ul>
        </li>
      </ul>

After clicking on the link everything is handled by the functions which get executed on click event. All I want to do that is make that a link, so that user can open it in new tab and state of the app should not change at all. How that is possible? I have tried to do that using routing but things became more complicated. i also followed some answers from here Make a HTML link that does nothing (literally nothing) but they do not work. Please help. I have tried all the answers from there opening link in new tab just disturbs the state of URL.

Sachin Singh
  • 61
  • 1
  • 7

3 Answers3

6

Using <a href="#"></a> will make the page scroll to the top.

Use javascript:void(0):

<a href="javascript:void(0)">Clicking here does nothing</a>

Or equivalently, use javascript:;:

<a href="javascript:;">Clicking here does nothing</a>

Or, with HTML5, just omit the href attribute:

<a>Clicking here does nothing</a>

You can use event.preventDefault() to prevent the default action of the link from being taken.

<a href="http://www.google.com">Valid Link</a><br>
<button id='hitMe'>Disable link</button>
<br/>
<button id="enable">Enable link</button>
<script>
document.getElementById('hitMe').onclick = function() { // button click
  document.querySelectorAll('a').forEach(a =>{
   a.onclick = function(e){
     e.preventDefault();
      return false;
    }
  });
};
document.getElementById("enable").onclick = function(){
  document.querySelectorAll('a').forEach(a =>{
   a.onclick = null;
  });
}
</script>
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
1

By clicking the button, no more links will be followed.

Like this you still have the hover destination preview. Like google.de etc...

document.getElementById('hitMe').onclick = function() { // button click
  [...document.querySelectorAll('a')].forEach(function(el){
    el.onclick = function( e ) { // all 'a'
      return false;
    }
  });
};
<a href="https://google.com">Valid Link</a><br>
<button id='hitMe'>Disable link.</button>
SirPilan
  • 4,649
  • 2
  • 13
  • 26
0

use

javascript:;

as href attribute and add

event.preventDefault()

onclick method.

<a href="javascript:;" onclick="event.preventDefault()">Click me</a>