js调用flex 当关闭窗口

<script type="text/javascript">
//-------------------------------------------------------------------------------------------   
//index.templete.html中写这个javaScript
// Specifies a function for the window's onbeforeunload event   
// Call back to the getUnsavedDataWarning in our Flex app when the browser is about to unload   
//-------------------------------------------------------------------------------------------   
//window.onbeforeunload = function()    

window.onbeforeunload = onbeforeunload_handler;  
function onbeforeunload_handler()    
{   
    var warning="";   
    var fxControl = document.${application} || window.${application};   
    if ( fxControl )   
        if ( typeof fxControl.getUnsavedDataWarning=="function")    
            warning = fxControl.getUnsavedDataWarning();    // This calls a function in our Flex app   
       //if ( typeof fxControl.getName=="function")    //getName是你注册的回调函数 ,判断是否是一个function是就调用
           // warning = fxControl.getName();    // This calls a function in our Flex app   
       
    if ( warning != '' )    
        return warning;   
    else    
        return void(0);    
}   
  
  

// Specifies a function for the window's onunload event   
// Call back to the javascriptOnUnload in our Flex app when the browser unloads   
   
window.onunload = function()    
{   
    var fxControl = document.${application} || window.${application};   
    if ( fxControl )   
        if ( typeof fxControl.javascriptOnUnload=="function")    
           fxControl.javascriptOnUnload();  // This calls a function in our Flex app   
}   

  

mxml中注册回掉函数
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete()">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			
	  private const UNSAVED_DATA_WARNING:String = 'You have unsaved changes. You will lose them if you continue.';   
               
         private function onCreationComplete():void {   
         	onClick();
           ExternalInterface.addCallback("getUnsavedDataWarning",   
            function():String {   
                return UNSAVED_DATA_WARNING;   
            }   
        );   
        
       
    } 
    	private function onClick(){
    		ExternalInterface.addCallback("getName",getName); //注册,第2个参数是function
    	} 
		
		public function getName(){
			mx.controls.Alert.show("js调用");
			return "nihoa"
		}
		]]>
	</mx:Script>
</mx:Application>

 

你可能感兴趣的:(JavaScript,html,xml,Flex,Adobe)