Simplyprofound.com
Flash, Flex, AIR, ASP.NET, PHP, AJAX, whatever it takes...

Flex Debugging - SecurityError #2060

April 1, 2008 17:18 by dave

I was wiring up an ExternalInterface call from my Flex application to javascript function in my HTML page today. It's really easy. But darn it if I could get the thing to work when debugging or running from Flex Builder. I kept getting the error:

SecurityError #2060: Security sandbox violation: ExternalInterface caller... (blah/blah/mxml cannot talk to blah/blah/html)

I was also getting an error in my javascript function which drew my attention. I was calling document.getElementById('myDiv').innterHTML and getting the error that my element "has no properties". Now, that's not a tough bit of code, so what was the deal? I was calling the function before the element was actually available in the page...it didn't exist yet. To verify this I put the function call inside of a window.onload function and viola, it worked.

So, back to the security error. I searched high and low and found dozens of 0 reply posts (there has to be a name for those poor souls left adandoned and alone - I think I'm the author of most of them!) where developers lamented the issue. 

The common situation: it works on the web server, but throws this error when running/debugging locally.

Flex Builder launches the application in your browser, and throws localhost on the front of the path which sounds like it's running in a virtual web server. It's not. Notice file://localhost. That means it's just reading the file locally, hence the security exception. 

It seemse to me that the only reasonable solution is to run a local web server and launch the Flex application to the local web path rather than the file path. Then your browser would be looking at http://localhost instead and the security issue would go away. 

To accomplish this setup your web server run a website from the bin-debug folder, then in Flex Builder modify your run/debug dialogue to open http://localhost/nameOfMyApp/myFile.html instead of the filepath that is there now.


Tags: ,
Categories: Flex
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

How to attachMovie in Actionscript 3

April 1, 2008 06:33 by dave

I'm converting an older Flash (AS2) kiosk application to an AIR (AS3) desktop application. The challenges are many, but it's a great learning experience. I'm reminded that it's the simple stuff that seems to matter most - those little tricks you use daily to make your projects go.

One AS2 feature that I used extensively in AS2 (as did the developer that originally wrote this particular project), was the attachMovie.

myTarget_mc.attachMovie("myLibraryMovieClip","newClip_mc",_root.getNextHighestDepth()); 

In AS3, this is dead and gone, and has been replaced with:

var newClip:MovieClip = new MyLibraryMovieClip();
myTarget_mc.addChild(newClip);

I have old habits, but I'm growing to like this.

BUT I still need to accomplish what was done in the original where I didn't KNOW the name of the object in the library. I knew if was a MovieClip, but the name was being passed into the function as a variable. How in the world do I create a new MovieClip based on a string variable?! 

Here is what I have discovered. You can retrieve the class that is created with linkage in the Flash library by name using getDefinitionByName(). Once you have that, you can create a new instance of that and add it to the stage or parent object. It looks like this:

import flash.utils.getDefinitionByName; 

var myLibraryMovieClip:Object = getDefinitionByName(variableString);
var newClip:MovieClip = new myLibraryMovieClip();
myTarget_mc.addChild(newClip);

LiveDocs for getDefinitionByName(): http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName()

And to give credit, I was turned on to this solution by a forum post over at Kirupa.


Tags: ,
Categories: Flash
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed