1

I'm trying to call a very simple JavaScript function from ActionScript but I can't seem to make it work. Here is my code:

JavaScript

function alert() {
    alert("hi");
}

ActionScript

ExternalInterface.call("alert");

HTML

<object width="500" height="500"
        classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"            
        codebase="http://fpdownload.macromedia.com/
        pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">
        <param name="SRC" value="FlashApp.swf"/>
    <param name="allowScriptAccess" value="always" />
    <embed src="FlashApp.swf" width="500" height="500" allowScriptAccess="always">                
    </embed>
</object>

2 Answers 2

1

Maybe it's because there is already an alert function? This works for me.

//js
function myAlert() {
    alert("hi");
}

//as3
ExternalInterface.call("myAlert");

The AS3 code :

public class Main extends Sprite 
{
    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point
        ExternalInterface.call("myAlert");

    }

}
Sign up to request clarification or add additional context in comments.

5 Comments

Check it out here : shadowytree.com/test It works for me when I use myAlert but if I change it to alert it doesn't work.
It worked for me too when I went to your link but it's not working on my page. Would you please post the source for the page you linked?
There you go : view-source:http://shadowytree.com/test/ put that in chrome to see it or you can just view source in your browser.
It's still not working, would it be possible to get the source of your ActionScript? At least the function where the call is made?
Ok that's weird, I finally got it to work by running it in IE
0

Make sure that the javascript is initialized before you call that method. Depends on luck, really, unless you work at it. I suspect Baris Usakli has a fast pc and the js is initialized faster than the swf is.

Use swfobject to embed the swf when the javascript onload event occurs. This way you KNOW the js function exists and you can call it.

Bonus points, swf object embeds it for most if not all browsers, with no fuss and with negligible overhead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.