javascript window.open不适用于safari和chrome [英] javascript window.open not working in safari and chrome

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

问题描述

我有一个div元素。
div是一个图标。
当你点击这个图标时触发表单提交。
在表单提交中有一些计算,并且这些计算的结果会打开一个新选项卡。



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



我试图创建一个隐藏的href并触发点击,所以它会打开新标签页,但它不起作用。



任何想法如何解决这个问题?



编辑 - 找到解决方案



您需要将window.open添加到$ .ajax成功方法中的单击事件中。

  $('#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上执行结果


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/

解决方案

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天全站免登陆