Search code examples
javascriptactionscript-3flashexternalinterface

Why is ExternalInterface not calling javascript - Flash CC and AS3


So, I've been avoiding posting this question because of the shear amount of prior posts. I've tried several variations of the following code, and to no avail.

I'm using FireFox (latest), Flash Player (latest), AS3, and A couple lines of JavaScript.

Here's my AS3 :

import flash.external.*;

// Handling Count For External Refreshing

var count: Number = 0;
var countMax: Number = 3;

function countHandler(): void {
    if (count >= countMax) {
        trace("The count has reached the max " + countMax);
        ExternalInterface.call("refresh");
        count = 0;
    } else {
        trace("The Count is " + count)
        return;
    }
}

I've traced the count, the total, all that, so I know that the code is working. It even resets the count when it gets to 3

Here's the javascript :

<script language="JavaScript">
    function refresh() {
        window.location.reload();
    }
</script>

For giggles/testing, I added a button to the page to test that the above code is working...

<input type="button" value="Reload Page" onClick="refresh()">

When I press the button, it refreshes the page.

Is there something I'm missing? Why will the working AS3 Not refresh the page by triggering the working javascript?

When I click the button, it refreshes, when the code triggers via ActionScript, I'm unable to press any of the SWF buttons. So it's firing, but not actually refreshing the whole page.

Update July4th - Updated AS3 and Java - Still no Luck


Solution

  • Thanks to Akmozo for the help.

    Set allowScriptAccess to always - NOT * Like the adobe tutorial says (bastards). The SWF can't have any hyphens or dashes. I had to set the file name to My.Test.File.fla so when I export/build it outputs the following flashContent

    <div id="flashContent">
        <object type="application/x-shockwave-flash" data="My.Test.File.swf" width="1920" height="1080" id="My.Test.File" style="float: none; vertical-align:middle">
            <param name="movie" value="My.Test.File.swf" />
            ...
            <param name="allowScriptAccess" value="always" />
            ...
        </object>
    </div>
    

    More info on symbols in nomenclature - Here

    Here is the ActionScript I used.

    // Handling Count For External Refreshing
    var count: Number = 0; //start count at 0
    var countMax: Number = 3; //max whatever you need it to be
    var isAvailable: Boolean = ExternalInterface.available; // checking if avail 
    
    trace(isAvailable); 
    
    function countHandler(): void {
        if (count >= countMax) {
            trace("The count has reached the max " + countMax); // Trace in flash 
            ExternalInterface.call("console.log", "testing..."); // Output to Console
            ExternalInterface.call("refresh"); //fire the refresh script
            count = 0; // reset the count
        } else {
            trace("The Count is " + count)
            return;
        }
    }
    

    Here is the java script. Placed in <head> below <style>

    <script language="JavaScript">
            function refresh() {
                console.log("Try To Refresh");
                window.location.reload();
            }
      </script>
    

    I had to create a crossdomain.xml file to allow access. I've tested the project several times with and without the crossdomain.xml and it without it, I get a security sandbox error. Not sure why allowScriptAccess="always" isn't working.

    Conclusion So it appears that setting allowScriptAccess to * is a bad idea. I got that from one of Adobe's Security Sandbox tutorials (thanks Adobe). Also, passing "refresh();" through ExternalInterface.call looks for refresh arguments which there are none. The SWf file name but not use dashes - like this..

    This... MyFileName.swf

    Not this... My-File-Name.swf

    This code is executing as expected. Thank you all for your input and help.