3 Replies Latest reply on Sep 7, 2007 3:25 PM by SymTsb

    How can I test if an URL is valid in Actionscript 3.0 ?

    Ardy15jan Level 1
      Hy, everyone!
      I`m trying to make a function that tests to see if an URL is valid and return true if it is, false otherwise, so that I can use it in an 'if' statement.

      The way I try to do it is by using two event listeners for the 'contentLoaderInfo' proprety of a Loader: one for Event.OPEN(if this event is dispatched, then I know the URL is valid) and one for IOErrorEvent.IO_ERROR(if this event is dispatched, then I know the URL is not valid).
      But the problem is that by using the event handler functions, I leave the main URL tester function, and am unable to return a value from it. The way I thought of to get around this problem was to set a returnValue variable, to which to assign the return value of the two event handler functions and then just go 'return returnValue;'. But I don`t know how to return a value from a event handler function, and even if I did I still couldn`t do it that way, because it takes some time to dispatch those events and the 'returnValue' variable would be undefined by the time I try to return it.
      I hope you understood what I tryed to explain above... Here`s some code to ilustrate this better(it`s not valid, it`s just so you understand my problem):

      private function testUrl(url:String):void {
      var returnValue:Boolean;
      var loader:Loader = new Loader();
      returnValue = loader.contentLoaderInfo.addEventListener(Event.OPEN, onGoodUrl);
      // This is a global variable to which I assign the value of 'loader' so that I can close it from onGoodUrl
      _testLoader = loader;
      returnValue = loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onBadUrl);
      loader.load(new URLRequest(url));
      // Even if I knew how to make an event handler that returns a value, I`d still have to wait for the loading to start, and I couldn`t return the value here:
      return returnValue;

      private function onGoodUrl(event:Event):Boolean {
      // ???
      return true;

      private function onBadUrl(event:IOErrorEvent):Boolean {
      // ???
      return false;

      You know any way to go around this problem? Or, for that matter, do you know any other way to test for a valid URL?

      Thank you for your time!