1 Reply Latest reply on Oct 13, 2009 4:55 AM by teocomi

    use navigateToURL  in a loop

    jensen.axel
      I'm trying to do a loop, and call a javascript function in flex for every loop.

      The loop only seems to call the javascript once the loop is done... and it only calls it for the last iteration of the loop.

      To reproduce the issue do the following:

      1. Create a new project in flex

      2. Put a button on the form, and make it's click="test1();"

      3. Create a script block like so

      <mx:Script>
      <![CDATA[
      private function test1():void{
      for (var i:int = 0; i < 5; i++){
      var u:URLRequest = new URLRequest("javascript:alert(" + "\'test " + i + "\');");

      navigateToURL(u,"_self");
      trace(i);
      }
      }
      ]]>
      </mx:Script>

      the button calls this very simple function, and theoretically you should get 5 alert boxes, but you don't...you get 1.... i'm curious if these methods can be used like this.
        • 1. Re: use navigateToURL  in a loop
          teocomi

          have a look here: http://livedocs.adobe.com/flex/gumbo/html/WS2db454920e96a9e51e63e3d11c0bf626ae-7fe8.html

           

          <s:Application 
              xmlns:fx="http://ns.adobe.com/mxml/2009"
              xmlns:mx="library://ns.adobe.com/flex/halo"
              xmlns:s="library://ns.adobe.com/flex/spark">
               <s:layout>
                <s:HorizontalLayout/>
               </s:layout>
            <fx:Script><![CDATA[
               import flash.net.navigateToURL;
               private var listingData:Array=[
                  "http://flex.org",
                  "http://www.adobe.com",
                  "http://aspexamples.adobe.com"
               ];

               private function openWindows(n: Number):void {
                  if (n < listingData.length) {
                     navigateToURL(new URLRequest(listingData[n]), '_blank');
                     callLater(callLater, [openWindows, [n+1]]);
                  }
               }
            ]]></fx:Script> 
            <s:Button label="Click Me" click="openWindows(0)"/>
          </s:Application>