2013-07-10 22 views
0

當HtmlLoader.load()失敗時,是否有辦法觸發錯誤事件?檢測HtmlLoad錯誤

當沒有互聯網連接時,COMPLETE事件永遠不會被觸發。我希望能夠告訴用戶「沒有連接,關閉當前窗口並執行其他操作。」

回答

0

當然可以,但你要聽的URLRequest實例一些事件

 //your code to load page in to HtmlLoader 
     var html:HTMLLoader = new HTMLLoader(); 
     var urlReq:URLRequest = new URLRequest("http://www.adobe.com/"); 
     configureListeners(urlReq); 
     html.width = stage.stageWidth; 
     html.height = stage.stageHeight; 
     html.load(urlReq); 
     addChild(html); 

    //add all listeners you need 
    private function configureListeners(dispatcher:IEventDispatcher):void { 
     dispatcher.addEventListener(Event.COMPLETE, completeHandler); 
     dispatcher.addEventListener(Event.OPEN, openHandler); 
     dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler); 
    dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler); 
     dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); 
     dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
    } 
+0

感謝。我試過了。我得到followng錯誤:1061:通過靜態類型爲flash.net:URLRequest的引用調用可能未定義的方法removeEventListener。 –

+0

爲什麼我不能使用removeEventListener?我想在IO_ERROR函數被調用後立即移除監聽器。 –

+0

現在,即使沒有使用removeEventListener,我也收到以下錯誤:1061:通過靜態類型flash.net:URLRequest的引用調用可能未定義的方法addEventListener。 –

-1

此代碼是工作的罰款對於Flex 4

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx" 
        creationComplete="creationCompleteHandler(event)"> 
<fx:Script> 
    <![CDATA[ 
     import mx.events.FlexEvent; 

     protected function creationCompleteHandler(event:FlexEvent):void 
     { 

      var html:HTMLLoader = new HTMLLoader(); 
      var urlReq:URLRequest = new URLRequest("http://www.adobe.com/"); 
      configureListeners(html); 
      html.width = 800; 
      html.height = 800; 
      html.load(urlReq); 
      conteiner.addChild(html); 

     } 

     //add all listeners you need 
     private function configureListeners(dispatcher:IEventDispatcher):void 
        { 
dispatcher.addEventListener(Event.COMPLETE, completeHandler); 
dispatcher.addEventListener(Event.OPEN, openHandler); 
dispatcher.addEventListener(ProgressEvent.PROGRESS,progressHandler); 
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler); 
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); 
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
     } 

     . 
        . 
        . 
        . 
        . 
        . 
    ]]> 
</fx:Script> 

<s:SpriteVisualElement id="conteiner"> 

</s:SpriteVisualElement> 
</s:WindowedApplication> 
+0

但是htmlloader類沒有IO_ERROR事件。請參閱Adobe文檔以供參考:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/html/HTMLLoader.html –

+0

嘗試將偵聽器添加到html.loaderInfo - 它應該包含您需要的所有事件http ://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/LoaderInfo.html – Vladimir