Translator
Categories
Archives

Archive for the ‘Flex’ Category

Flash Player Version Penetration Update

Adobe Flash Player Penetration by Version as of December 2008
  v7 v8 v9 v10
Mature Markets 99.1% 99.0% 98.6% 55.9%
US/Canada 99.1% 99.1% 98.9% 54.5%
Europe 99.1% 98.9% 98.2% 56.5%
Japan 99.0% 98.8% 98.3% 59.3%
Emerging Markets 98.7% 98.6% 98.1% 55.9%

Source: Adobe.com

Get QueryString Values in JavaScript

I have had several people ask me how to get URL QueryString values in JavaScript, so I created a function that will take a string, find the matching QueryString key, and return the value associated with that key. Enjoy!

NOTE: You may have to remove and re-type the “s from the code below if you copy it from this page. Some of them don’t come through correctly when you paste into your dev application.

Let's pretend the URL of this page is http://www.noobflash.com/foo/index.html?noob=someValue. The code above will find the first QueryString instance of "noob" in the URL and return the "someValue" portion to be used in your code. You would simply type queryStringValue("stringToFind") whenever you would like to get new QueryString data.

Load XML in Flash (AS3)

XML is the most popular data delivery methods for Flash and Flex. It’s fairly simple to load and consume XML, but it’s a little intimidating if you’re a ActionScript noob. Use the following code to pull in xml data and display it via a Flash trace.

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, getXML);
// Request XML File from URL
loader.load(new URLRequest("http://www.noobflash.com/feed/"));

function getXML(event:Event):void {
	var xmlData:XML = new XML(event.target.data);
	trace(xmlData);
}

Once you have the XML loaded, you will need to parse the XML using E4X. (Read: Parse XML Data in ActionScript 3 Using E4X)

Parse XML Data in ActionScript 3 Using E4X

E4X is a programming language extension that adds native XML support to ActionScript. This means that once you have some XML data loaded into Flash, you can reference and traverse that data by using E4X. For our example let’s assume that the XML below is what you have loaded into Flash. I’m assuming you already have a XML object assigned to a variable, but in this example I’m creating animalXML XML object. (Read: Load XML Using AS3)

var animalXML:XML = <animals>
    <animal species="feline" type="domestic">
        <name>Pookie</name>
        <age>5</age>
    </animal>
    <animal species="canine" type="domestic">
        <name>Spot</name>
        <age>8</age>
   </animal>
</animals>;

E4X Syntax Examples

animalXML.animal.name
- Returns all name nodes that are a child of the animal node
<name>Pookie</name>
<name>Spot</name>

animalXML.animal.@species
- A shortcut to select a node attribute instead of a node.
- NOTE: This shortcut only works if the attribute exists in all nodes in the list.
- For a more reliable method of attribute selection, use attribute(“species”) instead of @species.

animalXML.animal.(age >= 6)
- Parenthetical can filter nodes based on a given criteria.
- In this case, it will return all animal nodes that have an age of 6 or greater.

animalXML..age
- Using two dots (..) instead of one (.) will return all age nodes regardless of their depth in the XML node tree.
- This gives you the ability to search for descendants of a given node rather than children of it.

Using any JavaScript in Flash and Flex

Several months ago I needed to call a JavaScript function from within Flash. Unfortunately the flash file HAD to be iFramed (terrible, I know) inside of a page on another domain and there were tons of mind-numbing JavaScript cross-domain security violations that prevented anything from working correctly. Not having control of the other domain, I spent hours experimenting and eventually discovered a previously undocumented feature of ExternalInterface. Instead of writing JavaScript out on the page that contains your flash file, you can write JavaScript directly inside the ExternalInterface.call() function. This code has been successfully tested in both AS2 and AS3.

import flash.external.ExternalInterface;

private function close(){
ExternalInterface.call("function closeWindow() {alert('Closing Window')}");
}

This has actually come in hand several times since the fateful night it was discovered, so I hope it will come in handy for you as well.