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 中不起作用


基础教程推荐
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01