<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.24417-</id>
  <updated>2009-11-05T20:04:19Z</updated>
  <title>Comments for AIR: Tricks with mx:HTML (http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.24417</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=24417" title="AIR: Tricks with mx:HTML" />
    <published>2008-07-14T01:27:34Z</published>
    <updated>2008-07-14T17:25:41Z</updated>
    <title>AIR: Tricks with mx:HTML</title>
    <summary>In a number of cases, it is a lot easier to format text for display with HTML, rather than create an ActionScript based component to render complex-formatted text. This is especially the case when you have mixed content that should be displayed inline within the content (images, tables, text wrapping, etc...).  Here are a few tricks that you can use to fully take advantage of the mx:HTML control within your applications.</summary>
    <author>
      <name>Andrew Trice</name>
      
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[The mx:HTML control itself is pretty easy to understand... Its a control in the AIR tool set that enables you to display HTML based content in your AIR applications.  You can set the "location" property of the mx:HTML control to render a web page within your application, or you can set the htmlText attribute with HTML content to render it within your application.   <br /><br />

In a number of cases, it is a lot easier to format text for display with HTML, rather than create an ActionScript based component to render complex-formatted text.  This is especially the case when you have mixed content that should be displayed inline within the content (images, tables, text wrapping, etc...).   <br /><br />

Here are a few tricks that you can use to fully take advantage of the mx:HTML control within your applications:<br /><br />

<strong>Get rid of the default background on the HTML control so that it appears seamless withing your application.   </strong><br /><br />

You can set <strong>paintsDefaultBackground="false" </strong>and <strong>backgroundAlpha="0"</strong>, and the background for the html control will not be rendered.   You can use this approach to make your HTML content seamless with ActionScript based content in your applications.  Skins or animations that are in the background will be completey visible (unless you define a background in the HTML content that covers whatever is behind it).<br /><br />

You can use the mx:HTML control to display basically any text within your application; dynamic text messages, debugging messages, transaction logs, or just about anything else.   All of the formatting can be done in "plain old", easy to use HTML.   The HTML control itself is based on the <a target="_blank" href="http://livedocs.adobe.com/flex/3/html/help.html?content=AboutHTMLEnvironment_3.html">Webkit engine</a>, so why reinvent the wheel?  Let Webkit do the heaving lifting (and formatting)  for you.  <br /><br />

When you start dynamically generating HTML content in your application, you will start to run into some issues.   The first is that every time you set the "location" or "htmlText" properties, the control will re-render all of the HTML content and reset the scroll position to the top.   This will happen even if you are just performing a simple addition to the htmlText property, and could potentially cause serious performance issues:<br /><br />

<em>myHTML.htmlText += "add this text";</em><br /><br />

This will also happen if you use the load or loadString methods on the HTML control's htmlLoader property:<br /><br />

<em>htmlContent.htmlLoader.load( url );<br />
htmlContent.htmlLoader.loadString( myHTMLContentString );</em><br /><br />

An easy way to get around this is to use a combination of JavaScript within the HTML control, and ActionScript to dynamically append to the HTML content within the HTML control.   You can use DHTML techniques to append to the HTML content, which only causes a partial redraw, and does not lose your current vertical scroll position.   The big difference is that you will be invoking the JavaScript within the HTML container through ActionScript in the parent container.<br /><br />

First, let's look at the HTML/JavaScript that you can use to to dynamically append to the HTML content.   The following shows a simple HTML document that contains a JavaScript function "appendContent" and a HTML div that will contain the content.   The HTML content (value) passed into the function will simply be appended to the HTML content of the "content" div.<br /><br />

<big><div class="acode" style="overflow: auto; padding: 10px" ><div style="overflow-x: visible"> 
<code language="perl">
<pre>
&lt;body&gt; 
   &lt;script <span class="category2">language</span>="<span class="quote">Javascript</span>"&gt;  
      <span class="category1">function</span> appendContent( value ) 
      {  
          document.getElementById("<span class="quote">content</span>").innerHTML += value;  
       }  
   &lt;/script&gt; 
   &lt;div id="<span class="quote">content</span>" <span class="category2">name</span>="<span class="quote">content</span>" /&gt; 
&lt;/body&gt;</pre>
</code>
 
</div></div></big>

Now, let's take a look at how this can be applied.   The following is the source code for a simple AIR application that will allow the user to input text and append it to the HTML content:

<br /><br />

<big><div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;"> 
<code language="perl">
<pre>
&lt;?xml <span class="category2">version</span>="<span class="quote">1.0</span>" encoding="<span class="quote">utf-8</span>"?&gt;
&lt;mx:WindowedApplication 
   xmlns:mx="<span class="quote">http://www.adobe.com/2006/mxml</span>" 
   layout="<span class="quote">absolute</span>"
   defaultButton="<span class="quote">{ append }</span>"&gt;
   
   &lt;mx:Script&gt;
      &lt;![CDATA[
         
         <span class="category1">private</span> <span class="category1">function</span> appendToHTMLContent() : <span class="category1">void</span>
         {
             <span class="category1">var</span> content : <span class="category2">String</span> = textInput.<span class="category2">text</span>;
             
             content = content.replace( /&lt;/g, "<span class="quote">&amp;lt;</span>" );
             content = content.replace( /&gt;/g, "<span class="quote">&amp;gt;</span>" );
             
             content = "<span class="quote">&lt;div&gt;</span>" + content + "<span class="quote">&lt;/div&gt;</span>";
             
             htmlContent.htmlLoader.window.appendContent( content );
             
             textInput.<span class="category2">text</span> = "<span class="quote"></span>";
          }
         
      ]]&gt;
   &lt;/mx:Script&gt;
   
   &lt;mx:<span class="category2">Button</span> 
      <span class="category1">label</span>="<span class="quote">Append Text</span>" 
      top="<span class="quote">10</span>" right="<span class="quote">10</span>"
      id="<span class="quote">append</span>"
      click="<span class="quote">appendToHTMLContent()</span>" /&gt;
      
   &lt;mx:TextInput 
      top="<span class="quote">10</span>" left="<span class="quote">10</span>" right="<span class="quote">116</span>"
      id="<span class="quote">textInput</span>" /&gt;
   
   &lt;mx:HTML 
      id="<span class="quote">htmlContent</span>"
      location="<span class="quote">content.html</span>" 
      paintsDefaultBackground="<span class="quote">false</span>"  
      backgroundAlpha="<span class="quote">0</span>"
      bottom="<span class="quote">10</span>" top="<span class="quote">40</span>" left="<span class="quote">10</span>" right="<span class="quote">10</span>"/&gt;
   
&lt;/mx:WindowedApplication&gt;</pre>
</code>
 
</div></div> </big>

You can see that the HTML content simply loads the html document shown above <em>(content.html)</em>.   By default, nothing shows when this is loaded, because there  are no visible elements within the HTML.   In fact, the HTML control has actually loaded the html and the script that is used to append content can now be accessed:
<br /><br />

<big><div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;"> 
<code language="perl">
<pre>
&lt;mx:HTML 
	id="<span class="quote">htmlContent</span>"
	location="<span class="quote">content.html</span>" 
	paintsDefaultBackground="<span class="quote">false</span>"  
	backgroundAlpha="<span class="quote">0</span>"
	bottom="<span class="quote">10</span>" top="<span class="quote">40</span>" left="<span class="quote">10</span>" right="<span class="quote">10</span>"/&gt;</pre>
</code>
 
</div></div> </big>

The "appendToHTMLContent" ActionScript function takes the user input, replaces any angle brackets (&lt; or &gt;) with the HTML-encoded values, wraps it in a HTML "div", and then invokes the "appendContent" JavaScript function with the encoded user input.  You can access the JavaScript DOM (<a target="_blank" href="http://en.wikipedia.org/wiki/Document_Object_Model">Document Object Model</a>) for the loaded html through the htmlContent.htmlLoader.window property.  Using this technique, you can invoke any JavaScript function using ActionScript.

<br /><br />

<big><div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;"> 
<code language="perl">
<pre>
<span class="category1">private</span> <span class="category1">function</span> appendToHTMLContent() : <span class="category1">void</span>
{
 	<span class="category1">var</span> content : <span class="category2">String</span> = textInput.<span class="category2">text</span>;
 	
 	content = content.replace( /&lt;/g, "<span class="quote">&amp;lt;</span>" );
 	content = content.replace( /&gt;/g, "<span class="quote">&amp;gt;</span>" );
 	
 	content = "<span class="quote">&lt;div&gt;</span>" + content + "<span class="quote">&lt;/div&gt;</span>";
 	
 	htmlContent.htmlLoader.window.appendContent( content );
 	
 	textInput.<span class="category2">text</span> = "<span class="quote"></span>";
}</pre>
</code>
 
</div></div> </big>

This appends the user input onto the HTML content inside of the mx:HTML component, does not reset the scroll position, and only causes a minimal impact for redrawing the content.  <br /><br />

An added bonus for this approach is that if you want to allow the user to be able to select large "chunks" of content and copy it to the clipboard, then you don't have to do anything special.   This is already enabled within the HTML control.

<br /><br />
___________________________________<br />
<strong>Andrew Trice</strong><br />
Principal Architect<br />
<a href="http://www.cynergysystems.com" target="_blank">Cynergy Systems<br />
http://www.cynergysystems.com</a>


]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24417-comment:2020401</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24417" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2020401" />
    <title>Comment from Evan Mullins on 2008-08-06</title>
    <author>
        <name>Evan Mullins</name>
        <uri>http://blog.circlecube.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://blog.circlecube.com">
        <![CDATA[<p>Thanks! This helped me out.  It'd be nice to see the working example...</p>]]>
    </content>
    <published>2008-08-06T20:15:03Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24417-comment:2041969</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24417" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2041969" />
    <title>Comment from Matt Braun on 2008-08-28</title>
    <author>
        <name>Matt Braun</name>
        <uri>http://codehinting.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://codehinting.com">
        <![CDATA[<p>As far as I can tell, none of the HTML controls in AIR dispatch any sort of load progress events/other useful events. If anyone has insight into what object I can get that status from, or of an alternative to using the HTML control, I'd be glad to know!</p>]]>
    </content>
    <published>2008-08-28T18:43:02Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24417-comment:2045048</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24417" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2045048" />
    <title>Comment from Dante on 2008-10-29</title>
    <author>
        <name>Dante</name>
        <uri>http://ceoblog.pl</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://ceoblog.pl">
        <![CDATA[<p>Do You know how to prevent user from select and copy text from mx:HTML component ?</p>]]>
    </content>
    <published>2008-10-29T20:58:55Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24417-comment:2047625</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24417" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2047625" />
    <title>Comment from Jared on 2008-11-29</title>
    <author>
        <name>Jared</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>You say that you can load images this way.  How?  What path do you reference in the image src to bring in images that are in application directory?  I've tried:</p>

<p>src="app://images/foo.jpg"<br />
src="app:/images/foo.jpg"<br />
src="/images/foo.jpg"<br />
src="images/foo.jpg"</p>

<p>Thanks!<br />
</p>]]>
    </content>
    <published>2008-11-29T13:01:02Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24417-comment:2047741</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24417" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2047741" />
    <title>Comment from Ralph Krausse on 2008-11-30</title>
    <author>
        <name>Ralph Krausse</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Andrew,<br />
  <br />
    Any way to create a image from an HTML control using BitmapData?</p>

<p>thanks<br />
Ralph</p>]]>
    </content>
    <published>2008-11-30T13:50:45Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24417-comment:2057076</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24417" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2057076" />
    <title>Comment from Amit Batra on 2009-04-06</title>
    <author>
        <name>Amit Batra</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Is there any way to display long html page in air html control <br />
in pages not using vertical scroll bar i.e. if there is an html page of 1000 words , html control displays it in 4 pages having 500 words per page.</p>]]>
    </content>
    <published>2009-04-06T11:00:49Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24417-comment:2057077</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24417" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2057077" />
    <title>Comment from Andrew Trice on 2009-04-06</title>
    <author>
        <name>Andrew Trice</name>
        <uri>http://www.tricedesigns.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.tricedesigns.com">
        <![CDATA[<p>You can hide the scrollbar, but you would need to do the scrolling logic yourself.   I think the properties you will want to look at are "verticalScrollPosition" and "maxVerticalScrollPosition".</p>]]>
    </content>
    <published>2009-04-06T12:32:36Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24417-comment:2057479</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24417" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2057479" />
    <title>Comment from Bob White on 2009-04-13</title>
    <author>
        <name>Bob White</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>first-off... great example!</p>

<p>I'm having trouble re-rendering the html page after it initially loads.  for instance, if I append "AC_RunActiveContent" javascript code it seems that the content.html page does not run and do the document.write. </p>

<p>It seems that the "RunActive..." code only runs when the page initially loads. </p>

<p>Is there another way to refresh the htmlloader? thanks</p>]]>
    </content>
    <published>2009-04-13T21:42:05Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24417-comment:2065893</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24417" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2065893" />
    <title>Comment from Korn Matt on 2009-06-09</title>
    <author>
        <name>Korn Matt</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>None of the HTML controls in AIR dispatch any sort of load progress events/other useful events. If anyone has insight into what object I can get that status from, or of an alternative to using the HTML control, I'd be glad to know.<br />
Matt - <a href="http://www.club-penguin.org/">club penguin</a></p>]]>
    </content>
    <published>2009-06-10T01:42:00Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24417-comment:2068682</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24417" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2068682" />
    <title>Comment from Matt towe on 2009-07-20</title>
    <author>
        <name>Matt towe</name>
        <uri>http://www.typidee.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.typidee.com">
        <![CDATA[<p>Hi Andrew,<br />
  Thanks large for the article.  It's been a big help.  Unfortunately, I'm struggling with the same issue as Jared <a href="http://www.insideria.com/2008/07/air-tricks-with-mxhtml.html#comment-2047625">above</a>.  I've successfully loaded an html file from within the app package into an mx:html control.  However, I can't seem to load any resourced from*within* the HTML file.  Namely, I want to load images, CSS, javascript files etc.</p>

<p>I've tried every permutation of url scheme I can think of with no luck.  Have you run in to this and if so, do you know how to get it to work?</p>

<p>Thanks!</p>

<p>-Matt Towers</p>]]>
    </content>
    <published>2009-07-21T01:59:01Z</published>
  </entry>

</feed
