2

I'm having an issue on Chrome that doesn't happen on other browsers. When I run redirections in a loop, only the first redirection happens and the following ones are omitted.

E.g. the following code will only open 1 tab in Chrome, but will open 5 tabs in Firefox or Edge, among others:

for (var i = 0; i < 5; i++)
{
    window.open("https://google.com/")
}

What's the big deal with Chrome? Is there any sort of webkit function to let it open all the tabs I need on Chrome?

Héctor Álvarez
  • 494
  • 3
  • 18
  • When I try this in Chrome, I get a "Popup blocked" notification. When I allow popups and try again, it works perfectly fine. –  Jan 08 '18 at 12:56
  • @Kaddath I tried, but chrome.tabs returns undefined within my scope. It seems like I need to set permissions, but this isn't an app or a chrome extension, it's a js function that runs on click. – Héctor Álvarez Jan 08 '18 at 14:26
  • Ok you're totally right, sorry i read too superficially (removed the comment for future readers), the right explanation seems [to be rather here](https://stackoverflow.com/questions/16749907/window-open-behaviour-in-chrome-tabs-windows). Apparently you can't do more than one tab per user click, maybe you can trick the browser with events such as `mousemove` to simulate user action, but i doubt it a little – Kaddath Jan 08 '18 at 14:39
  • I managed to get an acceptable result with @ChrisG's solution, by enabling popups the agent allows this action. On the one hand, I can use this for internal use since it's a closed environment, I can also warn the user to allow popups and it works to some extent, on the other hand it's still a hack that I'd like to fix before I need to apply this on a full-scale deployment. Still accepting answers guys! – Héctor Álvarez Jan 08 '18 at 15:07
  • This questions still sounds like an [XY Problem](http://xyproblem.info/) to me; what is your actual goal here? How is opening tabs in a loop useful? –  Jan 08 '18 at 15:40
  • @ChrisG Good question, there's a functionality that already exists, which requests data from Web Service depending on a parameter selected from a dropdown list, and it's downloaded directly from the browser as per specification. The next requirement is an additional button that loops through the options available and downloads it one at a time, which will redirect as many times as options are available. The WS can't be edited (to accept a dummy value as "all available") and it must be done from the front-end. – Héctor Álvarez Jan 08 '18 at 17:11
  • @HéctorÁlvarez I'm not 100% sure I fully understood what you described there, but couldn't you do all that in a single window? Why do you need additional tabs to download/display multiple resources? –  Jan 08 '18 at 18:31
  • @ChrisG The system is designed to download files by redirection. Instead of using an actual form and downloading files properly, I have to retrieve the data from the API through a raw GET request, and now the requirements are "employees need a button that performs the download operation for each dropdown element, so they don't have to cycle through manually". I bet we are all thinking why, but that's what it is as per specification, plus it needs to work on every browser without using any frameworks (bye bye firefox datepicker), which leaves me with IE, Chrome and Edge... See the dilemma? – Héctor Álvarez Jan 09 '18 at 10:05
  • I see, but assuming that the server properly sets `Content-Disposition: attachment;`, you could use `window.location = [download_url];` to start the download, and give the user a button that will start each download in turn (or multiple buttons, one for each download). That way, no popups or new tabs needed. –  Jan 09 '18 at 10:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162808/discussion-between-hector-alvarez-and-chris-g). – Héctor Álvarez Jan 09 '18 at 12:26

1 Answers1

2

The only way to do this right now is allowing pop-ups at the client side.

Héctor Álvarez
  • 494
  • 3
  • 18