Press ESC to close popup window for Google Chrome
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
精彩评论