javascript window.open 在 safari 和 chrome 中不起作用 [英] javascript window.open not working in safari and chrome

查看:30
本文介绍了javascript window.open 在 safari 和 chrome 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 div 元素.div 是一个图标.当您单击此图标时,会触发表单提交.在表单提交上有一些计算,这些计算的结果会打开一个新选项卡.

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.

我使用window.open(url, '_blank');"

但在 safari 和 chrome 中,这个新标签被视为弹出窗口并被阻止.

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

我尝试创建一个隐藏的 a href 并触发点击,以便在新标签页中打开页面,但它不起作用.

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?

编辑 - 找到解决方案

您需要在 $.ajax 成功方法内的单击事件上添加 window.open.这样就可以了.

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/

推荐答案

你需要调用window.open作为onclick事件(用户事件)的直接结果......你不能在async之前做一些事情,然后这样做的结果是 window.open

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

这篇关于javascript window.open 在 safari 和 chrome 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆