DEV Community

Sriram
Sriram

Posted on

Interesting behavior of window.location.href

We recently had a small hiccup in downloading multiple files from a portal. We got blocked by browser saying its a popup or request client to allow multiple file download.

What are we trying to do ?

We have to download 3 files once client clicks on "Download all files" button. We don't want client to allow pop-up or allow multiple downloads

What are the options

  1. Use a library to download like filesync or saver

We don't like to use a library just to resolve this problem

  1. Use window.open

But this will be blocked by browser as pop-up.

  1. Use window.location.href

Well, if you use this, you can get it . Yet, if you trigger multiple urls with it, it will "override" the previous URL.

So, how to solve it

  • Use windown.location.href with settimeout

To read more about it, you can see this amazing article from Ryo Mac

https://medium.com/psynamic/demystifying-the-event-loop-featuring-settimeout-f702d4020d9a

This sets a delay in execution and helps with override of url.

Top comments (0)