javascript window.open not working in safari and chrome(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 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:javascript window.open 在 safari 和 chrome 中不起作用


- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06