Reputation: 31
We have a requirement on our tool wherein when a task is fetched, google search/amazon search pages should auto pop up as new tabs on same window. When the next task is fetched, the previous tasks tabs should close and the new task tabs should pop up. This is the implementation we have tried:
const [prevWindows, setPrevWindows] = useState([]);
const openWindows = (urls) => {
// Close all previously opened tabs of previous task
while (prevWindows.length > 0) {
prevWindows.pop().close();
}
// Open new tabs for each URL in the array and add them to the prevWindows array
const newTabs = urls.map((url) => window.open(url, '_blank'));
setPrevWindows(newTabs);
...
...
openWindows(autoPopUpTabUrlArray);
};
An array is sent from backend API to frontend which contains a list of urls to autopopup. An example array would be:
"autoPopUpTabUrlArray": ["https://www.google.it/search?q=cover iphone 11 mini con sora i disegni", "https://www.amazon.it/s?k=cover iphone 11 mini con sora i disegni", "https://www.amazon.it/dp/B0BTD7BNR5"]
The behaviour seen currently with above approach:
As we can see the google tab is not getting closed and remains open. Same behaviour continues and the google tabs are left open. Is there any reason why the google tab is not being closed or any alternate approach to achieve this?
Thanks.
Requirement: Google tabs should also close like the amazon/other url tabs. Tried checking online but did not find anything. Any help/insights would be appreciated.
Upvotes: 1
Views: 138