CallBack by CloseTab

Monkey Targets Forums/HTML5/CallBack by CloseTab

TFT(Posted 2013) [#1]
Hallo,

sorry vor my shit english. I have a question.

My HTML5 App mast do close the online conection befor the
BrowserTab close. OnSuspend() work it not under Win or Mac Browser .
Wat can i do ?

Thx TFT


MikeHart(Posted 2013) [#2]
OnSuspend IS called if you set #MOJO_AUTO_SUSPEND_ENABLED=True
inside the config.monkey file that is build during the build process.

But that detects only if the user clicks somewhere else so the focus is lost. If you close the tab, there is no buildin way to handle this.


therevills(Posted 2013) [#3]
You might be able to do it with an extern somehow, using the window.onbeforeunload or window.onunload callbacks...


TFT(Posted 2013) [#4]
Hallo,

great info .....

Thx TFT

PS: The window.onbeforunload work in the MonkeyGame.html perfectly,

<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
</script>

at the Main.JS not. Way?


impixi(Posted 2013) [#5]
Save the functions in a Javascript file and Import it in your Monkey code. Example:

unload.js

window.onbeforeunload = function()
{
	return "";
};

window.onunload = function()
{
	//Close connections, call destructors, etc...
};



runapp.monkey

Strict

Import "unload.js"

Function Main:Int()
	
	Print "Running..."

	Return 0
End



You can over-ride any HTML DOM event like that. However, sending data back to your calling Monkey code is more complicated: Probably create global javascript variables/objects and Extern them into your Monkey code.