flex 和 调用 js 接口
另有一个版本, 不提示直接保存<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" preinitialize="preinitializeHandler(event)" layout="horizontal">
<mx:Script>
<![CDATA[
import aw.external.jsinterface.JSFunction;
import aw.external.jsinterface.JSDynamic;
import aw.external.JSInterface;
static public const NOT_SAVED_ALERT:String = '\\\\n\\\\nYou have not saved data. Please, save data before close window.\\\\n Do you realy want to close window without save changes?\\\\n\\\\n';
protected function preinitializeHandler(event:Event):void{
JSInterface.initialize(this.systemManager.stage);
if(JSInterface.navigator.appName.toLowerCase().indexOf('fox')>0){
// For FireFox
JSInterface.window.onbeforeunload = this.isDataSaved;
}else{
// For Internet Explorer, FireFox, Safari or other
JSInterface.window.onbeforeunload = JSFunction.create("var text; if(!window.isFlexDataSaved){text='"+NOT_SAVED_ALERT+"';}; return text;");
}
JSInterface.window.isFlexDataSaved = false;
}
protected function isDataSaved(event:JSDynamic=null):String{
if(JSInterface.window.isFlexDataSaved) return "";
if(event){
event.returnValue = NOT_SAVED_ALERT;
}
return NOT_SAVED_ALERT;
}
]]>
</mx:Script>
<mx:Button label="Save Data" click="{JSInterface.window.isFlexDataSaved = true}"/>
<mx:Spacer width="100"/>
<mx:Button label="Change Data" click="{JSInterface.window.isFlexDataSaved = false}"/>
</mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" preinitialize="preinitializeHandler(event)" layout="absolute">
<mx:Script>
<![CDATA[
import aw.external.JSInterface;
import aw.external.jsinterface.JSDynamic;
import flash.utils.getTimer;
import mx.controls.Alert;
protected var _savedData:SharedObject;
protected function preinitializeHandler(event:Event):void{
JSInterface.initialize(this.systemManager.stage);
JSInterface.window.onbeforeunload = this.saveDataHandler;
this._savedData = SharedObject.getLocal('saved_data');
Alert.show('Saved data:', this._savedData.data.time);
}
// For Internet Explorer, Fire Fox, Safari and others
protected function saveDataHandler(event:JSDynamic=null):void{
Alert.show('save started');
var i:Number = getTimer();
while(getTimer()-i<3000); // generate and save data timeout
// you can not send data to server, but you can save data locally as temporary
this._savedData.data.time = (new Date()).time;
this._savedData.flush();
Alert.show('data saved as SharedObject');
}
]]>
</mx:Script>
</mx:Application>
评论