Link to home
Start Free TrialLog in
Avatar of dkilby
dkilbyFlag for Canada

asked on

OnKeyPress - spacebar

I want to be able to close the current window that has automatically been opened when the space bar is presse.  Is this possible and if so how?

Thanks
Avatar of Saqib Khan
Saqib Khan
Flag of United States of America image

<Script>
function kk() {
if(event.keyCode==32) {
//Close Window here
}

}
document.onkeydown=kk
</Script>
ASKER CERTIFIED SOLUTION
Avatar of Saqib Khan
Saqib Khan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nushi
Nushi

what do you mean by current window.

if you want to close window use:

window.close();

if you want to capture space bar event use:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
      <title>Untitled</title>
</head>

<body>
<script>

function catchSpaceBar(event){

      if(navigator.appName.indexOf('Netscape') != -1 ){
            if (event.keyCode == 32){
                  alert('Space was captured');
                  window.close();
                  }
            }
      else{
            if (window.event.keyCode == 32)      {
                  alert('Space was captured');
                  window.close();
                  }
            }
      
      }

document.onkeyup = catchSpaceBar;
      
</script>

!!! important:

For newer Netscape browsers,
their on-line manual says:
"To unconditionally close a window,
you need the UniversalBrowserWrite privilege."
(which really means a signed script).


</body>
</html>


Nushi.