Link to home
Create AccountLog in
JavaScript

JavaScript

--

Questions

--

Followers

Top Experts

Avatar of jarasa
jarasa🇪🇸

Disabling ALT-F4
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,'')
}

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of ZvonkoZvonko🇲🇰

Are you a XXX webadmin?

Avatar of jarasajarasa🇪🇸

ASKER


NO, Why?

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

:c)

Javier

Avatar of ZvonkoZvonko🇲🇰

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.



Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of jarasajarasa🇪🇸

ASKER

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

Avatar of jarasajarasa🇪🇸

ASKER

I think I could ask EE Administrator to delete the code part I put on the message too.

Avatar of ZvonkoZvonko🇲🇰

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().




Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of jarasajarasa🇪🇸

ASKER

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

Avatar of devicdevic🇩🇪

<script>
document.onkeydown = function(){
if(!window.event)return
if(window.event.keyCode == 18 || window.event.keyCode == 115)
      {
            alert(":)");
            event.returnvalue=false;
      }
}
</script>

Avatar of ZvonkoZvonko🇲🇰

Third strike beats it :)

But well done.


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of jarasajarasa🇪🇸

ASKER

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

Avatar of jarasajarasa🇪🇸

ASKER

Zvonko.

Is it possible or not?

At least you can save me headaches!!

Javier

SOLUTION
Avatar of ZvonkoZvonko🇲🇰

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

ASKER CERTIFIED SOLUTION
Avatar of devicdevic🇩🇪

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of jarasajarasa🇪🇸

ASKER

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

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.

JavaScript

JavaScript

--

Questions

--

Followers

Top Experts

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.