0

I working on a webpage.where i have number of links on the menu.When i clicked on the menu link it opens the page in a new tab. My requirement is that i have to prevent the new tab when i click on the link second time.

  1. When i clicked on the link first time it opens the page URL in new tab.
  2. When i clicked on the link second time it prevent the new tab ,if that URL is already open in the tab.
  3. If the URL is not open on the browser tab it opens the new tab.
  4. It the URL is open in new tab it will redirect it to already opened tab.

It should work without any external library with the basic java script.

<html>
<body>
<a id="aaa" href="https://stackoverflow.com" target="_blank">Visit stackoverflow.com</a> 
</body>
<script>
var links = document.getElementsById("aaa");
alert(links);
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");
};
}

0 Answers0