Wednesday, February 2, 2011

How To Close Pop up by pressing ESC Key


if you often use shortcut keys to navigate the web or any other
program for that matter, you probably expect the ESC key to close
popups and message boxes (alerts).When using popup windows on the web,
this is however not the case. If you use popups to show full sized images
of thumbnails, it would be natural to press the ESC key in the expectation
of the popup to close.To achieve this functionality, we have to place a little
JavaScript in the popup window. Add the following to the head section
of the html page:


<script type="text/javascript">

function ESCclose(evt) {

if (evt.keyCode == 27)

window.close();

}

</script>

Then add an onkeypress attribute in the body tag, like this:

<body onkeypress="ESCclose(event)">

That's all it takes and it's cross-browser compatible.

No comments :

Post a Comment