We are using a promise lib Q and we encounter the following issue. window.open() is blocked by browser when is called from promise
We try to open the window before the promise mechanism has started and this is working but the issue is that when a new tab is opened (and get the focus) all the browser resources are refer to the new tab and the logic is still done in the first tab which cause to bad performance.
There is other way to handle this maybe with event when the promise finished and then catch this event and open new window.
Update
What does it mean "all the browser resources are refer to the new tab" we found interesting behavior :) We are running application when user click on button, in this case we are open new tab and the focus is changed to the new tab and we are waiting to the app to start, this takes about 15 second (until the application actually running). We did the following test (which is very interesting :) ) and when we click on run application and the new tab is opens we are immediately click back to the first tab (which have the buttons) and by doing this the time is reduce to 4.5 seconds! It seems that the focus change the way that the resources is handled by the browsers.
I'm searching for alternative solutions!
We are using a promise lib Q and we encounter the following issue. window.open() is blocked by browser when is called from promise
We try to open the window before the promise mechanism has started and this is working but the issue is that when a new tab is opened (and get the focus) all the browser resources are refer to the new tab and the logic is still done in the first tab which cause to bad performance.
There is other way to handle this maybe with event when the promise finished and then catch this event and open new window.
Update
What does it mean "all the browser resources are refer to the new tab" we found interesting behavior :) We are running application when user click on button, in this case we are open new tab and the focus is changed to the new tab and we are waiting to the app to start, this takes about 15 second (until the application actually running). We did the following test (which is very interesting :) ) and when we click on run application and the new tab is opens we are immediately click back to the first tab (which have the buttons) and by doing this the time is reduce to 4.5 seconds! It seems that the focus change the way that the resources is handled by the browsers.
I'm searching for alternative solutions!
Share Improve this question edited Dec 29, 2020 at 15:48 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Oct 27, 2015 at 8:14 user4445419user4445419 8- What does "all the browser resources are refer to the new tab" mean? – jfriend00 Commented Oct 27, 2015 at 8:22
- @jfriend00 - its too long :) please see my update ,what do you think and how we can overcome it ? – user4445419 Commented Oct 27, 2015 at 8:31
- We can't help with your code issue without seeing the relevant code. Do you expect us to guess what the code is? One wild guess is that Javascript timers in background windows get slowed down while foreground window timers go full speed. But, since I have no idea what your code does, I have no idea if that is even relevant. – jfriend00 Commented Oct 27, 2015 at 8:34
- From the Stack Overflow guidelines: Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete and Verifiable Example. – jfriend00 Commented Oct 27, 2015 at 8:36
- @jfriend00 - I dont need a code help since this is already done and working as the link provided and your suggestion to open the window when at the button click which is working but the problem is that that we have in performance and my question is to open the window as suggested is the only way or there is additional ways which we can consider... – user4445419 Commented Oct 27, 2015 at 8:42
2 Answers
Reset to default 20This is because of a conflict among two policies:
By specification, promises execute
.then()
handlers asynchronously (after the current event loop has finished).For usability and security reasons
window.open()
can only be called from a direct user action (during the same event loop processing that was started by a user action or within a short time afterwards from some user action).
Because of #2, you will not be able to open a window from a .then()
handler of a promise that follows the promise specification.
The usual work-around is to open the window synchronously when the user clicks (before the promise has been resolved) and then to either fill in the content of the window you have already opened when the promise resolves or in error cases to close the window. This is not ideal, but if asynchronous operations are involved in the opening of a window, there really aren't other choices besides obtaining elevated privileges such as in a browser plugin (not from a normal web page script).
For more detailed help on how to solve specific coding issues with implementing this work-around, you will have to add your actual code to your question and describe the specific problems with it you are running into.
Questions asking for help with specific code must include the code you want help with.
This was my workaround. HTML:
<div id="next_to"></dif>
and within the promise
.then(() => {
var url = 'some place ...';
$("#next_to").bind( "click", function() {
window.open(url,"_parent");
};
$("#next_to").click();
});