Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript window.open not working in safari and chrome

I have a div element. The div is an icon. When you click on this icon is triggered a form submit. On the form submit there are some calculations and with the result of those calculations a new tab is opened.

I use the "window.open(url, '_blank');"

But in safari and chrome this new tab is seen as a popup and is blocked.

I tried to create a hidden a href and to trigger click so it will open the page in new tab, but it doesn't work.

Any idea how to fix this?

EDIT - SOLUTION FOUND

You need to add the window.open on a click event inside an $.ajax success method. In this way it will work.

$('#myButton').click(function () {
    var redirectWindow = window.open('http://google.com', '_blank');
    $.ajax({
        type: 'POST',
        url: '/echo/json/',
        success: function (data) {
            redirectWindow.location;
        }
    });
});

http://jsfiddle.net/safeeronline/70kdacL4/1/

like image 758
Pascut Avatar asked May 25 '17 12:05

Pascut


1 Answers

you need to call window.open as a direct result of a onclick event (user event)... you can't do something before async, and then on a result of that do the window.open

like image 157
UXDart Avatar answered Oct 04 '22 11:10

UXDart