flex 关闭浏览器
在Actionscript 2及以前,要打开任何网址,只需调用全局函数getURL()即可。在Actionscript 3中,已经取消了getURL()这个全局函数,取而代之的是flash.net包中的函数navigateToURL(),API格式如下:
public function navigateToURL(request:URLRequest,window:String=null):void
如果你想在flex应用中关闭浏览器窗口,可以利用navigateToURL调用javascript来实现,网上搜索得到如下示例:
引用
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
private function closeIE():void{
var request:URLRequest = new URLRequest("javascript:window.close()");
navigateToURL(request,"_self");
}
]]>
</mx:Script>
<mx:Button textAlign="center" label="Close current IE" click="closeIE()"/>
</mx:Application>
测试一下,你会发现关闭窗口之前会弹出确认的信息提示。如果想关闭窗口且不弹出信息窗口,将上述代码稍加改动(改动request变量声明那一行,注意黑体部分)即可实现。
引用
var request:URLRequest = new URLRequest("javascript:window.opener=null;window.close()");
实际上还可以更简单一些,如下:
引用
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Button textAlign="center" label="Close current IE" click="navigateToURL(new URLRequest('javascript:window.opener=null;window.close()'),'_self')"/> </mx:Application>
Alert.buttonWidth=60;Alert.yesLabel="是";Alert.cancelLabel="否";var alert:Alert=Alert.show("确定要退出系统吗?","提示",Alert.YES|Alert.CANCEL,this,function(event:CloseEvent):void{switch(event.detail){case Alert.YES://javascript:window.opener=null;window.close()");//没有确认对话框//var request:URLRequest = new URLRequest("javascript:window.close()");//有确认对话框//如果不加window.open('','_self') 则在IE7下会自动提示 是否关闭当前窗口 var request:URLRequest = new URLRequest("javascript:window.opener=null;window.open('','_self');window.close()");navigateToURL(request,"_self");break;case Alert.CANCEL:break;}},null,Alert.CANCEL);PopUpManager.centerPopUp (alert);var newX:Number = (Application.application.width-alert.width)/2; var newY:Number = (Application.application.height-alert.height)/2; alert.move(newX,newY);