How to open and then close a window without getting blocked as a popup?(如何打开然后关闭窗口而不会被阻止为弹出窗口?)
问题描述
我想指导电子商务网站中的客户通过 Paypal 的网站付款.我希望在新标签/窗口中完成付款,这样客户就不会丢失他/她所在网页的当前状态.
I want to direct a customer in an e-commerce site to pay via Paypal's website. I would like the payment to be done in a new tab/window so the customer doesn't lose the current state of the web page he/she is at.
为了让 Paypal 窗口打开而不会被阻止,我使用了 target="_blank" 的锚点.除了以下事实外,它工作得很好,因为 window.close()
不适用于未通过 window.open 打开的窗口,所以在 Paypal 付款完成后我无法关闭它()
.
In order for the Paypal window to open without getting blocked, I am using an anchor with target="_blank". Which is working perfectly except for the fact, I can't close it after Paypal payment is done since window.close()
doesn't work for windows that were not opened via window.open()
.
我怎样才能使它既不被阻止为弹出窗口,又可以在以后用 JS 关闭它?
How do I make it so it is BOTH not blocked as a popup AND I am able to close it with JS later on?
推荐答案
为了让 Paypal 窗口在不被阻止的情况下打开,我使用了 target="_blank" 的锚点.
In order for the Paypal window to open without getting blocked, I am using an anchor with target="_blank".
这是一种选择,但只要您在处理程序中为用户生成的事件(如 click
)调用 window.open
,就可以打开 pop-上窗户.因此,只需确保从链接上的 click
处理程序中调用 window.open
(然后您可以关闭它).现代弹出式窗口拦截器(过去几年的任何内容)会阻止不是由用户事件触发的弹出式窗口,但允许由用户事件触发的弹出式窗口.
That's one option, but as long as you call window.open
from within the handler for a user-generated event (like click
), you can open pop-up windows. So just make sure you call window.open
from within a click
handler on the link (and then you can close it). Modern pop-up blockers (anything from the last several years) block pop-ups that aren't triggered by a user event, but allow ones that are.
实例 |来源:
HTML:
<p><a href="#" id="target">Click to open popup</a>; it will close automatically after five seconds.</p>
JavaScript:
JavaScript:
(function() {
document.getElementById("target").onclick = function() {
var wnd = window.open("http://stackoverflow.com");
setTimeout(function() {
wnd.close();
}, 5000);
return false;
};
})();
这篇关于如何打开然后关闭窗口而不会被阻止为弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何打开然后关闭窗口而不会被阻止为弹出窗口?


基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01