Solution for HTML pop-up problem in AIR

24 04 2008

From the past few days, I’m seeing a problem with AIR HTML Component that is being reported by many users in many forums. So, i thought of giving a small solution which will help developers to overcome this problem.

Problem

AIR HTML Component fails to open new HTML pop-up window when the “target” of the link is set to “_blank” or “_new”.

Solution

The solution is very straight and simple.

Step 1 – Get all the available links of the page after it is completely loaded and add “onClick” event listener to all required links.

Step 2 – Get the Link’s URL in “onClick” event handler and open a new browser window using “navigateToURL()” or implement a custom window in AIR to handle this.

Code


<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:HTML id="htmlComp" width="100%" height="100%" location="http://www.rediff.com" complete="addEventListenersToLinks(event)"  />
 
 <mx:Script>
  <![CDATA[
   
   private function addEventListenersToLinks(e:Event):void
   {
    var dom:Object = e.currentTarget.domWindow.document;
    var links:Object = dom.getElementsByTagName("a");
    
    for(var i:Number = 0; i < links.length; i++)
    {
     if(links[i].target.toLowerCase() == "_blank" || links[i].target.toLowerCase() == "_new")
      links[i].onclick = linkClickHandler;
    }
   }
   
   private function linkClickHandler(o:Object):void
   {
    navigateToURL(new URLRequest(o.currentTarget.href),"blank");
   }
  ]]>
 </mx:Script>
</mx:WindowedApplication>

Advertisements