How do I send and retrieve variables between Actionscript 3 and Visual

Posted on 2009-04-16
Last Modified: 2013-11-11
Greetings Experts,

I need assistance understanding the basics of exchanging variables between a Flash .swf (actionscript 3) and a webpage.  I just need to exchange simple variables and I'm looking for the simplest solution as I am still learning both.

Code examples would be very helpful.

Question by:bham3dman
    LVL 11

    Accepted Solution


    You have a couple of options in getting a swf to talk to the outside browser world.  

    1) call a php web page -- may not want to do this if you're doing
    2) use ExternalInterface to call a JavaScript function.

    You probably want to do #2.  Have the AS code call a JavaScript function that communicates the variables to the page.  

    The adobe help for ExternalInterface includes a useful example

    Look up ExternalInterface in the index under 'E' (naturally :)   It's in the flash.external package.



    LVL 5

    Author Comment

    Hi Gene,

    Thanks for the response.  I did run across the ExternalInterface documentation in my initial search but  I'm afraid that I need a little more direction.  In my quest to figure this out, I've run across the code below but can't get it to work.

    I'm using swfObject to embed my swf and that works fine.  However, when I click the Home link, Flash isn't receiving the data from Javascript.

    In the actionscript, I have a couple of items - a movie clip that changes color and a dynamic text object to display the variable passed in. However, the function is never called.

    Any assistance is much appreciated.
    <html xmlns="" >
    <head runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript" src="scripts/swfobject.js"></script>
        <script type="text/javascript">
            swfobject.registerObject("myId", "9.0.0", "flash-java.swf");
        <form id="form1" runat="server">
        <script type="text/javascript">
            var currentPage="Home";
            function setCurrentPage(newPage) {
                currentPage = newPage;
            function getFlashMovieObject(movieName){
                if (window.document[movieName]){
                    return window.document[movieName];
                if (navigator.appName.indexOf("Microsoft Internet")==-1){
                    if (document.embeds && document.embeds[movieName])
                    return document.embeds[movieName];
                else // if (navigator.appName.indexOf(Microsoft Internet)!=-1)
                    return document.getElementById(movieName);
            function SendDataToFlashMovie(newPage)
                var flashMovie=getFlashMovieObject("myID");
      <a href="javascript:void(0);" click="setCurrentPage('Home')">Home</a>
      <object id="myId" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="550" height="400">
        <param name="movie" value="flash-java.swf" />
        <!--[if !IE]>-->
        <object type="application/x-shockwave-flash" data="flash-java.swf" width="550" height="400">
           <p>Alternative content</p>
            <!--[if !IE]>-->
    ----------actionscript 3----------
    import flash.external.*;  
    ExternalInterface.addCallback("sendTextToFlash", getTextFromJavaScript);
    function getTextFromJavaScript(str):void {
         txtStr.text = str;

    Open in new window

    LVL 11

    Expert Comment

    You may be having a security issue.

    You need to implement the example in the documentation I linked to above.  It will show you how things work and will alert you to security issues if you're having them.
    LVL 5

    Author Comment

    Thanks.  I'll try to implement the full example tomorrow.
    LVL 5

    Author Closing Comment

    I am sorry for the delay in accepting this.  Thank you for the help.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
    The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now