📜 ⬆️ ⬇️

How to bypass PopUp blocker in a browser in asynchronous requests

Problem


Security policy browsers are allowed to open a pop-up window, only if the user directly took some action to do this. For example, this code will work:

$("someElement").on("click", function(){ window.open("http://yandex.ru") } 


Now suppose that we need to make a request to the API in order to get a link and only then open a window with this link.
Thus, the code will no longer work, because the window is already opened not by the user's action:

 $.get("someURL").done(function(res){ window.open(res); }); 

')
Accordingly, you can solve the problem like this:
1. Obtain the necessary link in advance, but in this embodiment, the possibility of passing any parameters on the user's action to form a link is no longer possible.
2. make the request synchronous, which is not an option
3. think better

Third way


And what if you open the window in advance with a blank link, for usability and joy of the eyes, draw a loading indicator in the window, and after the link is formed, replace the location?

We try, everything works.

Actually a small piece of code:

  var win = window.open("", params) //   win.document       ,       $.get("someURL").done(function(res){ win.document.location = res; }); 


For other solutions, write in the comments.

Source: https://habr.com/ru/post/200096/


All Articles