简体   繁体   中英

Closing a tab using ascii codes

Ive read earlier that we cannot close the current tab using jQuery or Javascript.

My question is, Is it possible to use the ASCII codes for "ctrl+w" (that is 23) and calling it in jquery/JS to close the current tab?

I tried this code:

<script>
function closethis(){
    $(document).keypress(function(e) {
        if(e.which == 23) {
            alert('Are you sure you want to close?');
        }
    });
}
</script>

but doesnt work.. any leads??

No, you cannot close windows your script hasn't opened in any way.

(If your script opened a window with var newWindow = window.open(...) , you can close it with newWindow.close() .)

The most you can do in windows that you didn't open is show a dialog box when the tab is being closed by the user . For that, see How do I stop a page from unloading (navigating away) in JS? and unload on MDN .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM