在Actionscript 2及以前,要打开任何网址,只需调用全局函数getURL()即可。在Actionscript 3中,已经取消了getURL()这个全局函数,取而代之的是flash.net 包中的函数navigateToURL() ,API格式如下:
public function navigateToURL(request:URLRequest,window:String=null):void
如果你想在flex应用中关闭浏览器窗口,可以利用navigateToURL调用javascript来实现,网上搜索得到如下示例:
XML/HTML代码
- <? 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变量声明那一行,注意黑体部分)即可实现。
XML/HTML代码
- var request:URLRequest = new URLRequest(" javascript:window.opener = null ;window.close()");
实际上还可以更简单一些,如下:
XML/HTML代码
- <? 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 >