[英]Popup window opens but does not close automatically
I need to automatically close the window that I've opened with the javascript below. 我需要自动关闭使用以下JavaScript打开的窗口。 The popup window is opening correctly, but it does not close automatically in 1 second.
弹出窗口正在正确打开,但不会在1秒后自动关闭。
<script> function myFunction() { window.open("http://google.com", "_blank", "toolbar=no,scrollbars=no,resizable=no,top=50,left=250,width=300,height=150"); setTimeout("window.close();", 1000) } </script>
Can someone please help fix it. 有人可以帮忙解决它。
Thanks 谢谢
Your settimeout
is just calling a string, which is not a function. 您的
settimeout
只是调用一个字符串,而不是一个函数。
You also need to get a reference to the pop-up then call the close on the reference: 您还需要获取对弹出窗口的引用,然后对引用进行关闭:
<script>
function myFunction(e, anchor) {
e.preventDefault(); // Prevent navigation to page
var popup = window.open("http://google.com", "_blank", "toolbar=no,scrollbars=no,resizable=no,top=50,left=250,width=300,height=150");
setTimeout(function(){
popup.close();
// Continue navigating to link
window.location.href = anchor.getAttribute("href");
}, 1000);
}
</script>
<a href="https://yahoo.com" onclick="myFunction(event, this)"> <span class="tcb-button-texts">TRY IT NOW</span> </a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.