TDOT Blog Touchwood Creative

Register | Forgot password?

Author Info

Name
Ben Bishop

Job Title
Senior Interactive Developer



Touchwood is continuing to grow!

We are looking for talented Flex developers to join our Interactive Group. If you have any interest we would love to meet you. Please email your resume to us at:

jobs@touchwoodcreative.com


AIR: How to Detect All Link and Image Clicks in a HTMLControl

Problem:

How can an AIR application detect if a link or an image has been clicked inside an HTMLControl?

Solution:

Create an app to loop through all images and links in the web page's DOM and attach click listeners to them.

Execution:

  1. After creating and setting up the AIR application project, write an init function that will build our HTMLControl. This method is invoked by the app's "creationComplete" event:


    public function init():void{

    var container:UIComponent = new UIComponent();
    var html:HTMLControl = new HTMLControl();
    html.width = 500;
    html.height = 500;
    html.addEventListener(Event.COMPLETE, pageLoaded);
    var urlReq:URLRequest = new URLRequest("http://www.adobe.com/");
    html.load(urlReq);
    container.addChild(html);
    addChild(container);

    }

  2. Once the HTMLControl is done loading "http://www.adobe.com" it will call this method:


    private function pageLoaded(evt:Event):void{

    var numOfLinks:Number = evt.target.window.document.links.length as Number;
    for(var i:Number = 0; i < numOfLinks; i++){

    evt.target.window.document.links[i].onclick = linkClickHandler;

    }

    var numOfImages:Number = evt.target.window.document.images.length as Number;
    for(var j:Number = 0; j < numOfImages; j++){

    evt.target.window.document.images[j].onclick = imageClickHandler;

    }

    }

    This method interacts with web page's DOM (Document Object Model) to find out how many images and links there are. It then loops through the links and images arrays and adds a click listener to each link and image.

  3. Last, code the two event handlers:


    private function linkClickHandler(evt:*):void {

    mx.controls.Alert.show("link clicked");

    }

    private function imageClickHandler(evt:*):void{

    mx.controls.Alert.show("image clicked");

    }

  4. Viola!

Example project files are attached to this post. Feel free to leave any suggestions and/or comments.

AttachmentSize
HTML_Control_Events.zip284.73 KB

Your suggestion will work if

Your suggestion will work if I access the web page inside AIR app using HTMLControl. Is it possible to build an AIR app (agent) that monitors the html content on an external browser like IE or FireFox? If the web content has a particular image I would like to attach a listener to it's onclick event. I'm looking for such a solution to avoid coding separate extension for each browser.

In that case...

To do what you are describing will probably require using local area connection.

What you would do is have a Javascript function in the page you wish to monitor that adds an event listener to whatever HTML objects you wish to detect a click for. Once that event listener is invoked it will call a function in the embedded swf of that page. That embedded swf could possibly talk to AIR application via local area connection.

Please let me know if this helpful at all...

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
© Touchwood Creative 2007 | We use Firefox