开发者

Press ESC to close popup window for Google Chrome

开发者 https://www.devze.com 2023-03-07 16:46 出处:网络
I have this function for closing popup window by pressing the ESC escape key. However it is not working for Google Chrome. I don\'t know what is missing; does anyone have a solution ?

I have this function for closing popup window by pressing the ESC escape key. However it is not working for Google Chrome. I don't know what is missing; does anyone have a solution ?

function doClose(e) 
{
    if (!e) e = window.event; 

    if (e.keyCode) 
    {
        if (e.keyCode == "27") window.close();
    }
    else if (e.charCode) 
    {
        if (e.keyCode == "27") window.close();
  开发者_StackOverflow中文版  }
}
document.onkeypress = doClose;


onkeypress does not capture some keys - mainly system keys like ESC and F1 -> F12, use onkeydown instead. Also there's a bug in your logic, update to:

function doClose(e) 
{
    if (!e) e = window.event; 

    if (e.keyCode) 
    {
        if (e.keyCode == "27") window.close();
    }
    else if (e.charCode) 
    {
        if (e.charCode == "27") window.close();
    }
}
document.onkeydown = doClose;


Google Chrome bug:

http://code.google.com/p/chromium/issues/detail?id=9061

to WebKit Bug:

https://bugs.webkit.org/show_bug.cgi?id=25147

0

精彩评论

暂无评论...
验证码 换一张
取 消