javascript window.open not working in safari and chrome

☆樱花仙子☆ 提交于 2019-12-19 03:39:38

问题


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/


回答1:


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



来源:https://stackoverflow.com/questions/44180931/javascript-window-open-not-working-in-safari-and-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!