Page 1 of 1
Send pressKey when tab / browser closed
Posted: Thu Aug 23, 2018 4:41 pm
by ekidhardt
I'm trying to detect when the user closes a tab / browser - I need to send a pressKey back to have my program clear some files. What's the best way to go about this?
Re: Send pressKey when tab / browser closed
Posted: Thu Aug 23, 2018 5:02 pm
by Scott Klement
You didn't say what programming language/environment you are in. I guess I'll assume RPG since it's the most popular?
I guess it's possible to use something like a JavaScript event to send a "pressKey" or something like that when a window is closed, but I personally prefer a different approach. Consider the situation where someone's phone/laptop battery dies, or the power goes out... there's no way you can send a pressKey in situations like that. Likewise, if the HTTP server (Apache) times out and kills the job, there will be no JavaScript events involved.
A better solution, in my opinion, is to register routines that RPG or ILE will automatically run when things are ended.
Examples of possibilities are the CEE4RAGE() API, which runs a procedure when the activation group is ended. The "On-Exit" opcode, which runs code when a routine ends (no matter what ended it), the CEERTX/CEEUTX APIs allow a procedure to run when something is ended abnormally, etc. These allow you to do your custom code no matter how something got ended, so you don't have to worry abnout whether a pressKey() was sent or a timeout or a power failure... it runs in any situation.