1 Reply Latest reply on Jun 12, 2009 9:30 AM by Joe ... Ward

    Interacting with DOM objects using HTML

    Rehan Abdul Aziz

      Hi,

       

      I want to listen to "text selection" on a webpage in AS (i.e. as soon as a user highlights some text and releases the mouse button, an event gets fired). Events for cut, copy, and paste are available, but none for "text selection". I have read about cross-scripting between AS and JavaScript and understand it, although I do not understand yet how it can solve my problem. Also, is there any simpler way to do it directly in AS, without getting into JavaScript?

       

      Thank you very much

      Rehan.

        • 1. Re: Interacting with DOM objects using HTML
          Joe ... Ward Level 4

          There's a non-standard onselectstart attribute (or "selectstart" event type) that looks to be the only HTML DOM event dispatched for text selection.

           

          You can listen for such DOM events from ActionScript like so:

           

          package {
              import flash.display.Sprite;
              import flash.display.StageAlign;
              import flash.display.StageScaleMode;
              import flash.events.Event;
              import flash.html.HTMLLoader;

           

              public class HTMLLoadderTest extends Sprite
              {
                  private var html:HTMLLoader = new HTMLLoader();
                  public function HTMLLoadderTest()
                  {
                      this.stage.scaleMode = StageScaleMode.NO_SCALE;
                      this.stage.align = StageAlign.TOP_LEFT;

           

                      html.loadString("<html><body><p>Something to select.</p></body></html>");
                      html.width = this.stage.stageWidth;
                      html.height = this.stage.stageHeight;
                     
                      html.addEventListener(Event.COMPLETE, completeHandler);

           

                      addChild(html);
                      stage.nativeWindow.activate();
                  }

           

                  private function completeHandler(event:Event):void {
                      event.target.window.document.body.addEventListener("selectstart", reportSelection);
                  }

          //Note that you have to use Object as the parameter type because the JavaScript Event class is not the same as the ActionScript Event class
                  private function reportSelection( event:Object ):void
                  {
                      trace(html.window.getSelection());
                  }
              }
             
          }