Home Contact RSS

Style htmlText with CSS in your Actionscript | Flash/CSS Tutorial

Overview

In flash you can have text areas that are rendered as html. You can also apply formatting styles to this html. This will show a simple example on how to apply css to html text in flash. I’ll do a simple anchor tag style to show you the ropes. We’ll style a link to be underlined and then when you hover or mouse over it, we’ll change the color. It’s a design style that is widely used online in html, but flash doesn’t natively do it. As a matter of fact, flash doesn’t even natively underline links.

Steps

  1. Import TextField.StyleSheet
  2. create a style sheet object: var myCSS:StyleSheet = new StyleSheet();
  3. Specify your styles: myCSS.setStyle(”a:link”, {color:’#0000CC’,textDecoration:’underline’});
  4. Ensure that the text box is html enabled: myHTML.htmlText = myHTMLText;
  5. Apply the style sheet object to your html text box: myHTML.styleSheet = myCSS;

Example

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Actionscript

  1. import TextField.StyleSheet;
  2.  
  3. myHTMLText = "
  4. <h1>HTML Text (sample header)</h1>
  5. Here is some <em>sample</em> <strong>html text</strong> "+
  6. "filling a text box <a href="http://blog.circlecube.com">this link to circlecube</a> and example headers"+
  7. "<h1>Header h1</h1><h2>Header h2</h2>";
  8.  
  9. //create and initialize css
  10. var myCSS:StyleSheet = new StyleSheet();
  11. myCSS.setStyle("body", {fontSize:'15',color:'#000066'});
  12. myCSS.setStyle("h1", {fontSize:'25',color:'#000000'});
  13. myCSS.setStyle("h2", {fontSize:'19',color:'#000000'});
  14. myCSS.setStyle("a:link", {color:'#0000CC',textDecoration:'none'});
  15. myCSS.setStyle("a:hover", {color:'#0000FF',textDecoration:'underline'});
  16. myCSS.setStyle("b", {fontWeight:'bold'});
  17. myCSS.setStyle("em", {fontWeight:'bold'});
  18.  
  19. //ensure html support and apply css to it
  20. myHTML.html = true;
  21. myHTML.styleSheet = myCSS;
  22. myHTML.htmlText = myHTMLText;
  23. //resize the textbox to exact fit the text in it
  24. //myHTML.autoSize = "left";

Download

open source flashhtmlcss.zip

Tags: , , , , , ,

Calling actionscript functions through HTML text | asfunction Tutorial

Add this to the list of things I should have already known!

Story

I’ve got an html enabled text box and was trying to devise a way that I could have a hyperlink anchor tag not link to a webpage but actually do something flash. It didn’t seem possible, and I looked through all the different html css combinations I could think of. I finally resorted to trying to use some component like Deng or FlashML. FlashML had a smaller footprint and seemed to do more what I wanted, so I started investigating it. To my dismay, the support for it was few and far between. I found an older version that came with an example file and then a newer one with some documentation but no example and I found no examples any where else. So Lee, if you ever read this, some new examples could be nice. In the documentation I was reading about a functino called AddASFunction and the example html line was very interesting:

  1. <a href="asfunction:doSomething, startFrame">link</a>

I started looking through the rest of the documentation to find this asfunction use. But all it had was:
The href attribute can include the asfunction string which allows the link provided by the anchor to call a function in Flash. More of this can be found within the addASFunction definition in this help document.
I knew I was on to something, asfunction. So a quick google search and I found the official doc! I was shocked that I had the tool to do this the whole time! Well, shocked and feeling like an idiot for never having heard of it before. I knew it could be done somehow, but had no idea that it was already a feature of htmlText in flash! So now that you know my embarrassing story, I’ll let you in on the secret.

Overview

In flash, you can allow html text within a text area. You either set the text html property as true with actionscript (my_txt.html = true;) or click the ‘Render text as HTML’ button in the properties window of the text area. You cannot enable html text on static text areas however. You can have links and various html elements (but not full html). Usually links have a url in the href attribut of the anchor tag, but flash will read a special value of ‘asfunction’ which specifies that an actionscript function is to be called rather than a url. The correct syntax is asfunction followed by a colon and then the name of the actionscript function to be called, optionally followed by a comma and a possible single argument to be passed to the specified function (href=”asfunction:functionName,argument”).

Steps

  1. Enable html in the text box.
  2. Have your function (ex: functionName) ready to be called from the html link.
  3. Give the href attribute of the anchor tag a property “asfunction:functionName,argument” Notice that the official documentation calls for spaces after punctuation, but any space you put after the colon (:) or comma (,) will be sent to the function in the argument, or will expect a space in the function name and give you a headache.

Example

In this example I’ve got an html enabled text box with 4 links. The first is a standard link (I hope you know what that does). The next link calls an actionscript function with asfunction. The third link sends a single argument to another function. And the last link sends multiple arguments to yet another function. Wait! Multiple arguments? I thought I said only one was supported, well this example shows how to send multiple arguments disguised as a single param and parse them. It’s pretty simple actually.
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Actionscript

  1. import TextField.StyleSheet;
  2.  
  3. myHTMLText = "Sample text in an html enabled text box. "+
  4. "Here's a normal link to <a href='http://blog.circlecube.com' target='_blank'>circlecube</a>! "+
  5. "And some more links that don't go anywhere, they call functions in actionscript. "+
  6. "<a href='asfunction:clickLink'>Click this one</a>, "+
  7. "to see the actionscript function called from the html text box. "+
  8. "<a href='asfunction:clickWithArg,Click this too'>Click this too</a>, "+
  9. "and see that the actionscript function you're calling can have an argument passed to it. And "+
  10. "<a href='asfunction:clickWithMultipleArgs, one,two,three args'>click me three and four</a> "+
  11. "to see a way to send multiple arguments from your htmlText. "+
  12. "Also, one last example of what not to do "+
  13. "<a href='asfunction: clickWithArg, arg with preceding space'>Click for nothing</a>";
  14.  
  15. //create and initialize css
  16. var myCSS:StyleSheet = new StyleSheet();
  17. myCSS.setStyle("a:link", {color:'#0000CC',textDecoration:'none'});
  18. myCSS.setStyle("a:hover", {color:'#0000FF',textDecoration:'underline'});
  19.  
  20. myHTML.html = true;
  21. myHTML.htmlText = myHTMLText;
  22. myHTML.styleSheet = myCSS;
  23.  
  24. //function to be called from html text
  25. function clickLink() {
  26.     giveFeedback("Hyperlink clicked!");
  27. }
  28.  
  29. //another function to be called from html text, recieves one argument
  30. function clickWithArg(arg) {
  31.     giveFeedback("Hyperlink clicked! Argument: "+arg);
  32. }
  33.  
  34. //a simple trick to allow passing of multiple arguments
  35. function clickWithMultipleArgs(args) {
  36.     giveFeedback("Hyperlink clicked! Multiple arguments passed: "+args);
  37.     argArray = new Array();
  38.     argArray = args.split(',');
  39.     for (i = 0; i < argArray.length; i++) {
  40.         giveFeedback("arg "+i+": "+argArray[i]);
  41.     }
  42. }
  43.  
  44. function giveFeedback(str) {
  45.     trace(str);
  46.     feedback.text += str +"\n";
  47.     feedback.scroll = feedback.maxscroll;
  48. }

HTML

  1. Sample text in an html enabled text box.
  2. Here's a normal link to <a href='http://blog.circlecube.com' target='_blank'>circlecube</a>!
  3. And some more links that don't go anywhere, they call functions in actionscript.
  4. <a href='asfunction:clickLink'>Click this one</a>,
  5. to see the actionscript function called from the html text box.
  6. <a href='asfunction:clickWithArg,Click this too'>Click this too</a>,
  7. and see that the actionscript function you're calling can have an argument passed to it. And
  8. <a href='asfunction:clickWithMultipleArgs, one,two,three args'>click me three and four</a>
  9. to see a way to send multiple arguments from your htmlText.
  10. Also, one last example of what not to do
  11. <a href='asfunction: clickWithArg, arg with preceding space'>Click for nothing</a>

Download Source

asfunction.zip

Tags: , , , , , , ,

Intro to Flashvars | Passing variables to actionscript from the html embed | Tutorial

I’ve had a couple special requests to explain flashvars and how to use it and show it in action.

Overview

The property “FlashVars” can be used to import root level variables to the flash movie or swf. The flashvars propery is used in codes for embedding flash in the html page. The string of variables passed in as flashvars, will be imported into the top level of the movie when it is first instantiated. Variables are created before the first frame of the SWF is played. The format of the string is a set of name=value combinations separated by ampersand (&) symbols.

Steps

  1. Include the flashvars property in your embed codes and voila! You have these variables to use in your swf.
  2. That’s the one step

Code

HTML Embed Codes

  1. Here's some sample embed codes, including object and embed tags:
  2. <object width="540" height="240" title="sample">
  3.   <param name="movie" value="flashvarsTutorial.swf" />
  4.   <param name="flashvars" value="var1=here&var2=are&var3=my&var4=flashvars" />
  5.   <embed src="flashvarsTutorial.swf" flashvars="var1=here&var2=are&var3=my&var4=flashvars" type="application/x-shockwave-flash" width="540" height="240" ></embed>
  6. </object>

Actionscript using flashvars

  1. //flashvars="var1=val1&var2=val2&var3=val3";
  2.  
  3. display("var1 = "+ var1);
  4.  
  5. display("var2 = "+ var2);
  6.  
  7. display("var3 = "+ var3);
  8.  
  9. display("var4 = "+ var4);
  10.  
  11. function display(todisplay:String){
  12.   feedback.text += todisplay+"\n";
  13.   trace(todisplay);
  14. }

Example

Page 1 (var1=val1&var2=val2&var3=val3)
Page 2 (var1=here&var2=are&var3=my&var4=flashvars)

Source

Download the html files and the fla and swf in this flashvars.zip

Tags: , , , , , ,

Using CSS Attribute Selectors to Stylize HTML | Style outbound links | Tutorial

Intro to CSS

We use css to apply styles to certain elements on the page, we can target any div like this:

HTML

  1. <div>Text</div>

CSS

  1. div {
  2. css-property: value;
  3. }

Any class selector <div class=”divClass”> like this:

HTML

  1. <div class="divClass">Text</div>

with this:

CSS

  1. div.divClass {
  2. css-property: value;
  3. }
  4. <!-- or simply -->
  5. .divClass {
  6. css-property: value;
  7. }

or any id selector, <div id=”divID”> like this:

HTML

  1. <div id="divID">Text</div>

with this:

CSS

  1. div#divID {
  2. css-property: value;
  3. }
  4. <!-- or simply -->
  5. #divID {
  6. css-property: value;
  7. }

These are the basics of css. Use an element tag name to target it, use a dot to access class names and a hash (#) to represent id names. A lot can be done with just that, but sometimes you may want to access something differently, an option is to use attribute selection.

Overview

More advanced we can apply styles to elements based on their attributes. Attribute selectors use the attributes of the tag.
We can use attribute selection to specify certain elements to stylize. For example if we have a page with many images but only certain ones have title attributes, which we want to stand out more, this css rule would do the trick:

CSS

  1. img [title] {
  2. border: 2px solid #000000;
  3. }

It would cause any image with a title tag (no matter what the value of the title tag is) to have a 2px wide solid black border, such as <img title=”MyImage” src=”/images/sample.jpg” /> or <img title=”" src=”/images/sample.jpg” /> but not <img src=”/images/sample.jpg” /> because it has no title attribute.

HTML

  1. would style
  2. <img title="”MyImage”" src="http://blog.circlecube.com/wp-admin/”/images/sample.jpg”" alt="" />
  3. or even
  4. <img title="”&quot;" src="http://blog.circlecube.com/wp-admin/”/images/sample.jpg”" alt="" />
  5. but not
  6. <img src="http://blog.circlecube.com/wp-admin/”/images/sample.jpg”" alt="" />
  7. because it has no title attribute.

Further we can specify which values of the title attribute we want to target. If we want to stylizee links to a certain site we can do this: a[href="http://blog.circlecube.com"] { }

CSS

  1. a[href="http://blog.circlecube.com"] {
  2. background-color: #EBEBEB;
  3. }

it would style <a href=”http://blog.circlecube.com”>This link</a> but not <a href=”http://blog.circlecube.com/2008/05/21/”>this one</a> because it is not an exact match, nor <a href=”http://www.google.com”>this one</a> because it isn’t a match either, or at all.

HTML

  1. it would style
  2. <a href="”http://blog.circlecube.com”">This link</a>
  3. but not
  4. <a href="”http://blog.circlecube.com/2008/05/21/”">this one</a>
  5. because it is not an exact match, nor
  6. <a href="”http://www.google.com”">this one</a>
  7. because it isn’t a match either, or at all.

For another example, if we want to stylize local links differently than absolute links, we’d want to look at the beginning of the attribute’s value only so we’d use ‘^=’. We could have something like this:
a[href^="http://"], a[href^="https://"] {
background: url(/images/external.gif) no-repeat right center;
padding-right:20px;
}
it would style <a href=”http://www.google.com”>This link</a> because it begins with ‘http://’ but not <a href=”/2008/05/21/”>this one</a> because it is does not begin with ‘http://’. But it would also style <a href=”https://paypal.com”>this</a> because it matches the selector after the comma ‘https://’, and even <a href=”http://blog.circlecube.com/2008/05/21/”>this</a> will be styled, because the link is absolute (even though it is local) so be careful with how you use it.

HTML

  1. it would style
  2. <a href="”http://www.google.com”">This link</a>
  3. because it begins with ‘http://’ but not
  4. <a href="http://blog.circlecube.com/wp-admin/”/2008/05/21/”">this one</a>
  5. because it is does not begin with ‘http://’.
  6. But it would also style
  7. <a href="”https://paypal.com”">this</a>
  8. because it matches the selector after the comma ‘https://’,
  9. and even
  10. <a href="”http://blog.circlecube.com/2008/05/21/”">this</a>
  11. will be styled, because the link is absolute
  12. (even though it is local) so be careful with how you use it.

Summary

Hoping you will see the pattern and can use the rest of these somehow (I’m drawing blank on interesting examples),

1 is: [attribute] exists

target anchors with any titles attributes.

CSS

  1. a[title] {
  2. background-color:#0000FF; (blue)
  3. }

HTML

  1. <a title="some title" href="http://blog.circlecube.com/">Link</a>

2 equal: [attribute=x] equals x

target only anchors where the title attribute contains something exactly

CSS

  1. a[title="Only"] {
  2. background-color:#FF0000; (red)
  3. }

HTML

  1. <a title="Only" href="http://blog.circlecube.com/">Link</a>

3 hat: [attribute^=x] starts with x

target instances where something comes at the beginning of the attribute. This can prefix a word or even be the first word in a phrase or sentance.

CSS

  1. a[title^="Super"] {
  2. background-color:#00FF00; (green)
  3. }

HTML

  1. <a title="Supercalafragalisticexpialadosious" href="http://blog.circlecube.com/">Link</a>

4 dollar: [attribute$=x] ends with x

instances where something comes at the end of the attribute. This can be the suffix of the word or the last word in a phrase.

CSS

  1. a[title$="ious"] {
  2. background-color:#FFFF00; (yellow)
  3. }

HTML

  1. <a title="Supercalafragalisticexpialadosious" href="http://blog.circlecube.com/">Link</a>

5 asterisk: [attribute*=x] contains x

or even titles which contain a certain word somewhere/anywhere in the attribute. This wildcard be anywhere, in a word, as a word, whatever.

CSS

  1. a[title*="tic"] {
  2. background-color:#FF00FF; (magenta)
  3. }

HTML

  1. <a title="Supercalafragalisticexpialadosious" href="http://blog.circlecube.com/">Link</a>

6 tilde: [attribute~=x] one of which is exactly x.

a space-separated list of “words”, one of which is exactly x.

CSS

  1. a[title~="tic"] {
  2. background-color:#FF00FF; (magenta)
  3. }

HTML

  1. <a title="Super cala fragalis tic expi ala dosious" href="http://blog.circlecube.com/">Link</a>

7 pipe: [attribute|=x] which first word is exactly x.

a hyphen-separated list of “words”, first word is exactly x.

CSS

  1. a[title|="Super"] {
  2. background-color:#FF00FF; (magenta)
  3. }

HTML

  1. <a title="Super-cala-fragalis-tic-expi-ala-dosious" href="http://blog.circlecube.com/">Link</a>

view all examples on this page.
refer to w3 for more

Let me know what you come up with or if I’ve left out any essentials.

Tags: , , , , ,

Going Natural 3.0 at StomperNet

Here’s a new site and series from StomperNet called Going Natural 3!
It’s a bit of free videos made and released to showcase the talents and business of what StomperNet is about and what they do for their clients. They’re ‘moving the freeline’ so to speak…

The first video series begins with Dan Thies talking about his ‘Crazy Theory’ for AdWords.

On signing in there are a couple BONUS videos for you as well. So go check them out as well!
Watch Going Natural 3 - Adwords Triangulation Method and more

This site contains the latest flash video player built by yours truly. I also did the design of the site: involving html, css, php, javascript and dealing with drupal too!

Tags: , , , , , , , , , , , ,

Get Current URL and Query String Parameters to Flash | Tutorial

Overview

This tutorial / how to / example will show how to get the current url from the browser to flash, and even how to get the query string parameters from the url into actionscript using ExternalInterface.
It has been a dilemma for many people to get this information into flash across browsers and without having to rely on flashvars or javascript, but to just have it work.
I wrote a post on it earlier, although it seemed it wouldn’t play nice with Internet Explorer IE, I later realized that it was only because of the way my blog is configured to embed flash. The call ExternalInterface.call(”window.location.href.toString”); or even ExternalInterface.call(’eval’, ‘window.location.href’); which basically do the same thing.
This can be taken even further and we can read the query string, which, if you don’t know what that is, is the data contained in the url. The data is sent as paired strings, the key and the value. So, for example I could have a url http://example.com/index.html?var1=one&var2=two&var3=three. The question mark separates the actual url path from the query string. So following the ‘?’ we see three variables: var1, var2 and var3, and their corresponding values: one, two and three. They are seperated as pairs with an ampersand (&) and then the key and value are seperated by an equals sign (=). So it goes url?key=value&key=value&key=value…
Once we pass the complete url into our swf, it’s pretty easy to parse the keys and corresponding values.

Steps

  1. Rather than use url with ExternalInterface.call(”window.location.href.toString”); implement the QueryString class make a new QueryString This will do most of the work for you: var myPath:QueryString = new QueryString();
    1. Upon creation of the QueryString object the class reads the parameters automatically by parsing the parameters after the ‘?’ and delimiting on the ‘&’. So you get var1=one and var2=two
    2. Set up each parameter (key) as a variable in the parameter object of the QueryString class assigning it’s value to that variable.
  2. Access your values as myPath.parameters.var1 and myPath.parameters.var2
  3. unescape() your values to make the usable, unless you need them to be encoded or course. Unescape decodes the string from URL-encoded format (converting all hexadecimal sequences to ASCII characters). If your parameter had been some funky encoded string like var4=this+stuff%3E%22%28%29%3F, after you unescape(myPath.parameters.var4) you get: this stuff>”()?.

Example

get url params screenshot
Here’s a working example. This link has the parameters appended to it following the question mark ‘?’ and separated with an ampersand ‘&’ like all query string parameters. I have one for myName (Circlecube) another for myText (Jo Jo is a monkey) which are both pulled out and put into their own text box after they are unescaped, and then there are a couple more parameters just to show, the aNum (3013), anotherParam (more), and ref (http://blog.circlecube.com/…)

Special thanks to Abdul Qabiz example. I rewrote it for as2 so it would work with some flash projects I’m working on.

I use the new swf object 2 to embed the swf. Go get it here: swfobject

Actionscript:

The actionscript layer of the swf

  1. import flash.external.*; //so we can use externalInterface
  2. import QueryString.as;   //so we can use the QueryString Class//make a new QueryString named myPath
  3. var myPath:QueryString = new QueryString();
  4. assignVariables();
  5.  
  6. //custom function to handle all the query string parameters
  7. function assignVariables() {
  8. //if myName parameter exists
  9. if (myPath.parameters.myName) {
  10. //assign it to the text of the myName text box
  11. //unescape() will translate/unencode the url characters
  12. myName.text = unescape(myPath.parameters.myName);
  13. }
  14. if (myPath.parameters.myText) {
  15. myText.text = unescape(myPath.parameters.myText);
  16. }
  17. if (myPath.url) {
  18. //get the complete url (including any parameters)
  19. thisUrl.text = myPath.url;
  20. }
  21. recurseTrace(myPath.parameters, " ");
  22. }
  23.  
  24. //function to recursivly print objects in heirarchy as string
  25. //so we get all parameters no matter what the key traced into
  26. //the allParams text box.
  27. function recurseTrace(info:Object, indent:String) {
  28. for (var i in info) {
  29. if (typeof info[i] == "object") {
  30. traceParams(indent + i + ":");
  31. recurseTrace(info[i], indent + " ");
  32. }
  33. else {
  34. traceParams(indent + i + ": " + info[i] + "\n");
  35. }
  36. }
  37. }
  38.  
  39. function traceParams(traceMe:String) {
  40. allParams.text += traceMe;
  41. }

The QueryString.as class for as2

  1. class QueryString {
  2. //instance variables
  3. var _queryString;
  4. var _all;
  5. var _params:Object;
  6.  
  7. public function QueryString() {
  8. readQueryString();
  9. }
  10. public function get getQueryString():String {
  11. return _queryString;
  12. }
  13. public function get url():String {
  14. return _all;
  15. }
  16. public function get parameters():Object {
  17. return _params;
  18. }
  19.  
  20. private function readQueryString() {
  21. _params = {};
  22. try  {
  23. _all = ExternalInterface.call("window.location.href.toString");
  24. _queryString = ExternalInterface.call("window.location.search.substring", 1);
  25. if(_queryString) {
  26. var allParams:Array = _queryString.split('&amp;');
  27. //var length:uint = params.length;
  28.  
  29. for (var i = 0, index = -1; i &lt; allParams.length; i++) {
  30. var keyValuePair:String = allParams[i];
  31. if((index = keyValuePair.indexOf("=")) &gt; 0) {
  32. var paramKey:String = keyValuePair.substring(0,index);
  33. var paramValue:String = keyValuePair.substring(index+1);
  34. _params[paramKey] = paramValue;
  35. }
  36. }
  37. }
  38. }
  39. catch(e:Error) {
  40. trace("Some error occured. ExternalInterface doesn't work in Standalone player.");
  41. }
  42. }
  43. }

Download

Here’s a zip file containing the sample files, the QueryString Class file, and even the swfobject javascript file.
getURLParams.zip

Tags: , , , , , ,

CSS and HTML WYSIWYG in Flash | Open Source Example

Overview:

Using what I learned with the Actionscript Javascript Communication Tutorial, and pushing it a little further I’ve set up this example of how flash renders html and css. This is basically a wysiwyg (What you see is what you get) html editor! Natively flash only handles some html and css. Many people have enhanced it’s capabilities with projects and Classes, but I made this to show what is accepted by default as far as html and css is concerned. I know there are specs and many lists about what will work, but to me the best way to know if my code will work is to try and see…
I’ve made this app so if I have a question, I just paste in my html/css and send it to the swf to see it rendered live. This saved me a few headaches, so I thought other might enjoy it as well… So here it is.

Example:

Render your own html and or css in flash. htmlToFlash.html
Here is the flash rendering of some dummy text as html with css applied
htmltoflash thumb

Here’s the html interface where I paste in the html and css.
htmltoflash thumb 2
Each supported css property has a corresponding actionscript property, but the naming convention is a little different for css in actionscript. Each actionscript property name is derived from the corresponding CSS property name; the hyphen is omitted and the subsequent character is capitalized. So for example: ‘font-weight’ becomes ‘fontWeight’.

Download:

Here’s the open source files if you want to get your hands dirty. Let me know if you improve this or even have any questions about it!

Again, note there are only certain HTMl and CSS supported by flash, follow the links for more info.
HTML supported by Flash and CSS supported by Flash

Tags: , , , , , , , , ,

Actionscript Javascript Communication | ExternalInterface call and addCallback Tutorial

Overview:

Using ExternalInterface.addCallback() we can expose an actionscript function and make it available to javascript. This would be helpful to have your webpage with embedded flash communicate to your flash swf file and even control it with javascript! Say we wanted to have buttons in the html page that would control an object in the flash. Communication is a two-way road so wouldn’t it be great to be able to go the other way as well, you can! That is the main function of ExternalInterface! In this example/tutorial I will explain both directions of communication with flash and javascript! Communication between flash and javascript isn’t just a myth or mystery!

Steps:

  1. Be sure to import flash.external.*;
  2. Set up the javascript to actionscript lane of your communication road. (ExternalInterface.addCallback(methodName, instance, method);)
  3. Write your javascript function.
  4. Set up the actionscript to javascript lane. (ExternalInterface.call(functionNameInJavascript);)

We will follow the text’s journey on our road of communication…

The One way: I type in ‘Johnny Appleseed’ in the html text box and press the Send Text To Flash button. The onclick javascript event finds the flash element and calls it’s function (sendTextFromHtml) and then clears the text in the html box. This function has been set up and is exposed to javascript (in actionscript lines 4-7) with the methodName ’sendTextFromHtml’ while the method it calls is recieveTextFromHtml() in the actionscript. So ‘Johnny Appleseed’ is received as the parameter of the recieveTextFromHtml() function and is assigned to the text of the theText text box.

And back: Now I delete ‘Johnny Appleseed’ since he’s only a fable and enter ‘Paul Bunyan’ in the swf text box and press the Send From Flash to Javascript button. This calls the onRelease function associated with this button. ExternalInterface.call calls the ‘recieveTextFromFlash’ function in the javascript of the page and passes ‘Paul Bunyan’ as the parameter. The javascript function finds the html text box using getElementById() and assigns the parameter to the value of that text box!

This technique will even work if you’re not sending folklore character down the road.

Example:

View the live example here: ActionscriptJavascriptCommunication.html

Actionscript Javascript Communication thumbnail
Actionscript:

  1. import flash.external.*;
  2.  
  3. //Set up Javascript to Actioscript
  4. var methodName:String = "sendTextFromHtml";
  5. var instance:Object = null;
  6. var method:Function = recieveTextFromHtml;
  7. var wasSuccessful:Boolean = ExternalInterface.addCallback(methodName, instance, method);
  8.  
  9. //Actionscript to Javascript
  10. //ExternalInterface.call("recieveTextFromFlash", _root.theText.text);
  11.  
  12. function recieveTextFromHtml(t) {
  13. _root.theText.text = t;
  14. }
  15.  
  16. _root.button.onRelease = function() {
  17. ExternalInterface.call("recieveTextFromFlash", _root.theText.text);
  18. _root.theText.text = "";
  19. }

Javascript:

  1. function recieveTextFromFlash(Txt) {
  2. document.getElementById('htmlText').value = Txt;
  3. }

HTML: view Source of sample page

Download:

Download all source files (.fla, .html, .swf): ActionscriptJavascriptCommunication.zip

Tags: , , , , , , , , ,
Next entries »