Home > Blockchain >  Visit Multiple Links in the Same New Window
Visit Multiple Links in the Same New Window

Time:10-18

I want to visit multiple links in the same new window.

Lets say I open google.com in a new window after that I open yahoo.com but this has to open in the same window where google.com was opened by re-loading the same google.com window.

I have this website as a reference http://doubleadvertise.com just trying to achieve something similar to this website.

<a href="https://www.google.com/" target="_blank">google</a><br>
<a href="https://www.bing.com/" target="_blank">bing</a><br>
<a href="https://www.yahoo.com/" target="_blank">yahoo</a>

How to achieve this thanks

CodePudding user response:

You can do this using some basic JavaScript. You can use the Window.open() method to open a new window. This method also returns a handle to the opened window so you can perform actions such as setting Window.location.href which will redirect the tab to a different page. You can still use the _blank target like you're doing in your HTML.

let newWindow = null;

function openLink(link){
  if(newWindow === null){
    newWindow = window.open(link, '_blank');
  }
  else{
    newWindow.location.href = link;
  }
}

openLink('https://google.com/');
setTimeout(() => openLink('https://yahoo.com/'), 2000); // Delay it 2 seconds so previous link as time to open
setTimeout(() => openLink('https://bing.com/'), 4000); // Delay it 4 seconds so previous link as time to open

I can't include this code as a runnable snippet because popups are blocked, so here's a JSFiddle. Note you will need to enable popups.

Keep in mind that this will not work for websites that have the Cross-Origin-Opener-Policy header set to same-origin-allow-popups (Google Translate for example) as outlined in this answer. There is no workaround, you will just have to avoid these websites.

  • Related