Sep 26, 2008

Warn on flex application exit

If you have been developing web based applications which require login, I am pretty sure you would have come across this question - How do you safe-logout the user if the user just closes the window/tab when there is unsaved information on the user’s screen?

My current Flex project requires the user to login and logout. And most of the time, the users never use the logout button. They just close the browser and walk away! :-x So, I needed a mechanism which will warn the user that there is unsaved information before closing the window. The way, I got this working was by using the FABridge.

In the application, I had an isAppDirty boolean.

public var isAppDirty:Boolean = false;

This variable is set to true whenever there is unsaved information in the application from any view. Now, add a few lines of javascript to your html wrapper. You can do this to the html file in the html-template folder.

————————————————————————————-

window.onbeforeunload = confirmExit;

function confirmExit(){
var flexApp = FABridge.flash.root();
var appDirty = flexApp.isAppDirty();

// note that the isAppDirty var is called like a function. This is done deliberately. Thats the way FABridge works :)

if(appDirty == true){
return “The configuration changes performed in this session have not been saved. If you wish to save the config, please click CANCEL.”;
}
}

————————————————————————————-

Do not forget to include the FABridge libraries in the html -


Thanks,

Shaleen Jain

No comments:

Post a Comment

Post a Comment