我希望在网页上创建一个链接,点击该链接可以关闭浏览器中当前激活的标签页而不关闭其他标签页。当用户单击关闭链接时,应出现一个警告消息,提示用户确认是否关闭,提供两个按钮,“是”和“否”。如果用户单击“是”,则关闭该页面;如果单击“否”,则不执行任何操作。
如何实现?有什么建议吗?
如何实现?有什么建议吗?
<script>
function Exit() {
var x=confirm('Are You sure want to exit:');
if(x) window.close();
}
</script>
<button>
调用该函数。<button name='closeIt' onClick="Exit()" >Click to exit </Button>
As stated previously by RixTheTyrunt here, due to security, Modern browser behavior does not allow window.close to work unless is opened by window.open or under other strict circumstances(like opening a new tab with minimal interaction).
When a page attempts to post a form, once that form interacts; window.close does not work(at least for me in several browsers: ff, edge, chrome...).
I liked a lot the solution by Thailandian, but contains document.write which is frown upon.
I tried several solutions from this page and from other, but is by design.
This is what I considered a workaround. The main idea is to let the User know.
Best regards,
Thanks for reading.
<script>
// below will attempt to close if possible
function closing() {
window.close(self);
window.close('', '_parent', '');
window.close('', '_self', '');
// after several attempts will let the User know
// below, will display a Full Screen Div with a Message for the User
document.getElementById("msgDiv").style.display = 'block';
// below, will Hide your Current Page Div to allow only the Previous Message
document.getElementById("currentPage").style.display = 'none';
// Below a timer to reload
setTimeout(function() {
document.location.reload(true);
}, 3000);
}
</script>
<!-- Below is the Message for the User -->
<div align="center" id="msgDiv" style="display:none;width:100%;height:100vh">
<h1><br>Unable to close Tab/Window with Scripts.<br><br>Please Close Tab/Window Manually in the Browser.<br><br>Thanks.<br><br><button style="cursor:pointer" onclick='document.location.reload(true);'>Reload</button></h1>
</div>
<!-- Below is the Current Page -->
<div id="currentPage" style="display:block;">
<div style="position: absolute; right:1%; top: 2px; height: 22px; width: 30px; padding: 0em;" title="EXIT">
<a href="#" onclick="closing()" style="cursor:pointer;text-decoration: underline;font-weight:900; background: tomato; border: 4px outset red; border-radius: 4px;">×</a></div>
Your Page Content Should be Here <br>
<br>
<br>
<form method="post" action="#">
<div align="center" style="padding:14%;">
<input style="font-size:1.5em" type="text" name="text" placeholder="Favorite Word" autofocus="autofocus">
<input style="font-size:1.5em;cursor:pointer" type="submit" value="Go"></div>
</form>
<pre style="white-space:pre-wrap;">
As stated previously by RixTheTyrunt here, due to security, Modern browser behavior does not allow window.close to work unless is opened by window.open or under other strict circumstances(like opening a new tab with minimal interaction).
When a page attempts to post a form, once that form interacts; window.close does not work(at least for me in several browsers: ff, edge, chrome...).
I liked a lot the solution by Thailandian, but contains document.write which is frown upon.
I tried several solutions from this page and from other, but is by design.
This is what I considered a workaround. The main idea is to let the User know.
Best regards,
Thanks for reading.
</pre>
</div>
<a href="javascript:if(confirm('关闭窗口?'))window.close()">关闭</a>
1
您只能关闭使用JavaScript打开的选项卡(如Ryan Joy所提到的)。2
,这里没有提到:只有当window.top.opener
(“父”窗口)不为null时,您才能关闭选项卡。 - Nico