如何打开然后关闭窗口而不会被弹出窗口阻止? [英] 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.open()
打开的窗口中window.close()
无效,因此在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
,就可以打开弹出窗口.因此,只需确保从链接上的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;
};
})();
这篇关于如何打开然后关闭窗口而不会被弹出窗口阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!