Create new tab/window from JavaScript

Dec 4, 2015
>>>

Yesterday I was stuck in a very strange situation. It was required to open a tab from a success handler of a server call. Actually browser lets open a new tab/window from JavaScript if the ‘opening code’ is directly inside of a user event handler. May be a click, may be blur event or focus event, even page load. Otherwise it understands that the client side code is trying to open a new tab/window unauthorized; and the in-built popup blocker blocks the opening of the same; one browser specific indication of-course shown that a popup has been blocked.

After a little search I came across one solution to make the server call synchronous, then the new tab/window ‘opening code’ would be directly in the user event and there will be no problem in the intended purpose.

Keeping the above consideration in mind, came with one more solution, one solution that can by-pass the situation.

Solution 1:

Make the server call synchronous, so that the new tab/window opening code be directly in the user event handler. (plunker)

Solution 2:

Create one window, in the same closure, as the server call is made and keep the reference to it in a variable. And in the success handler of the call, change the location of the window created in-prior or close the same in error handler. (Closing the window may be a problem for Chrome & Firefox) (plunker)

Solution 3:

In the success handler create one confirmation dialog for the user, and in the ‘OK’ handler create the intended new tab/window.

There may be another solution to create a form with target=”_blank” and in the action we will do the intended server call. But, I’ve not tried it. And this may not be a very good approach.

Blog comments powered by Disqus