How to close current tab in a browser window?

2018-12-31 03:17发布

I want to create a link on a webpage that would close the currently active tab in a browser without closing other tabs in the browser.
When the user clicks the close link, an alert message should appear asking the user to confirm with two buttons, "YES" and "NO". If the user clicks "YES", close that page and If "NO", do nothing.

How can it be done? Any suggestions?

11条回答
爱死公子算了
2楼-- · 2018-12-31 03:42

This method works in Chrome and IE:

<a href="blablabla" onclick="setTimeout(function(){var ww = window.open(window.location, '_self'); ww.close(); }, 1000);">
    If you click on this the window will be closed after 1000ms
</a>
查看更多
裙下三千臣
3楼-- · 2018-12-31 03:43

Try this as well. Working for me on all three major browsers.

<!-- saved from url=(0014)about:internet -->
<a href="#" onclick="javascript:window.close();opener.window.focus();" >Close Window</a>
查看更多
春风洒进眼中
4楼-- · 2018-12-31 03:43

Here's how you would create such a link:

<a href="javascript:if(confirm('Close window?'))window.close()">close</a>

查看更多
孤独总比滥情好
5楼-- · 2018-12-31 03:44

As far as I can tell, it no longer is possible in Chrome or FireFox. It may still be possible in IE (at least pre-Edge).

查看更多
梦醉为红颜
6楼-- · 2018-12-31 03:45

Try this

<a href="javascript:window.open('','_self').close();">close</a>
查看更多
登录 后发表回答