We help IT Professionals succeed at work.

Disabling ALT-F4

jarasa
jarasa asked
on
19,252 Views
Last Modified: 2007-12-19
Hi all.
Does anybody know a way to disable ALT-F4 key that really works without making any change to the current document or opening a new window, my code works and it disable the key but I have to display an alert and open a new window or it closes the main one. Besides if I push ALT-F4 again while the alert is open it closes the main window too, I would like to do it without making any of this things.

Tahnks in advantage

Javier

P.D.: Here is how I do it.

document.onkeydown=teclas;  

function teclas() {  

var tecla = window.event.keyCode;
var control = window.event.ctrlKey;
var alt = window.event.altKey;
var tipo=event.srcElement.type;

if (alt) {
      switch (tecla) {
            // Alt-F4
            case 115:
                  window.event.keyCode=86;  
                  window.cancelEvent=true;
                  event.cancelBubble=true;
                  event.returnValue=false;
                  event.cancel=true;
                  adios();
                  return false;  
                  break;
      }
}
}

function adios() {
             // I have to display this alert first
      alert("VEGA :: Opcion no Valida")
             // And open this window too or it wont work
      openCerrar("cerrar.jsp",400,250,null,null,'')
}
Comment
Watch Question

ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Are you a XXX webadmin?

Author

Commented:

NO, Why?

To tell you the truth I don't even know what you mean with XXX webadmin

:c)

Javier
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
It means pink pages.

And it can be true that you do not serve such pages, but other readers here could use such anoing things, like you ask for, for anyting they want.


Author

Commented:
OK I see.

Well I'm doing an Intranet application that I want it to work as a client server one and I want to disable user from closing the application without pushing the "Exit" button wich make some validations and invlidates the sesion on the server, the way I told you works but I prefer not to show any message to the client, I do control all the keybord keys, and don let anyone to take any part of the information from the window, I don't know if is wrong or not to do this but if you know the way and don't want to show it here you could sent it to my by mail at ramos_j1@tsm.es, I will appreciate it a lot I've been figuring it out for more than a week and my time cost a lot to my user.

:c)

Javier

Author

Commented:
I think I could ask EE Administrator to delete the code part I put on the message too.
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
For that situations is better to maintain page state and evaluate the state in unload event.
Did you test with body attributes:
<body onLoad="initState()" onUnload="checkState()">

In checkState() function you can notify your server side about session termination, for example in a popup window wich ends with self.close().



Author

Commented:
I know that I can call the fucntion to disble session on the unload of the document but beside that I don't want the user to close the window with ALT-F4 I have a window made out of 8 frames to simulate a client disign window in 7 of the 8 frames I have no code so if the focus is at one of those I' will have to control to many things, actually I have it done and working with that code but it the user pushes ALT-F4 on the alert it closes the main window. Of course the user can press Ctrl-Alt-Del and kill the task but well I don't know if they'll go that far.

I don't mind deleting the question if nobody is going to answer it.  But Zvonk, I know what I want to do!!

Thanks anyway for the replays.

Javier
CERTIFIED EXPERT

Commented:
<script>
document.onkeydown = function(){
if(!window.event)return
if(window.event.keyCode == 18 || window.event.keyCode == 115)
      {
            alert(":)");
            event.returnvalue=false;
      }
}
</script>
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Third strike beats it :)

But well done.

Author

Commented:
Thanks devic, but that does not work either as I'm trying. besides that I'd like to display no messages at all if you keep ALT key pressed and push twice F4 key it closes the window.

I'm not really sure if it is possible but I think it is or at least I remember I got a page once on the internet that I only coulded close killing the task.

Javier

Author

Commented:
Zvonko.

Is it possible or not?

At least you can save me headaches!!

Javier
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Well Thanks Zvonko, I was begining to think that you knew the solution but weren't going to tell me, I've been searching the net for more than a week and my code is the most i've been able to figure out, I know it has to be a way but....

Devic, thanks for you effort either, I like that solution but I don't think I'll be able to implement it, onbeforeunload event is fired anytime you change a page and I have 200 JSP on my application, the message will appear everytime I change from one part of the application to another becose I close all windows and open a new one when the user change from one part of it to another.

Well guys I'll slit the points on between you, I guess is the best thing I can do, but if you get to it please let me know it, OK.

Thanks again for the effort.

Javier

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.