2

I've been all over here and can't find an answer. I have a .swf sitting in an HTML page and I am trying to call a function inside of it from javascript. I can talk out from flash to the javascript but I can't get it to talk back in. I know I am targeting the object properly because I use console.log() on it and confirms what it is targeting.

I'm triggering the test from flash, calling a javascript function from inside the .swf, and having that function call the internal Flash function.

Flash Code:

//adds callback
ExternalInterface.addCallback("sendToFlash", flashTalkedTo);

//function called by the callback
public function flashTalkedTo():void{
    //runs another function in javascript to log a string
    ExternalInterface.call("callMe")
}

//calls javascript that tries to talk to Flash
ExternalInterface.call("catchFromFlash")

Javascript Code:

//function called by Flash that initiates
function catchFromFlash(){
    talkToFlash()
}

//function that tries to talk to flash
function talkToFlash(){
    document.getElementById('Noodleverse').sendToFlash()
}

//function called by Flash in the end to confirm call made
function callMe(){
    console.log("Call Me")
}

Any help works, thanks!

2
  • 2
    which HTML element has the Noodleverse id? Showing the rough HTML structure would help. Commented Mar 29, 2013 at 18:14
  • And also doing a console.log(document.getElementById('Noodleverse')) inside talkToFlash to confirm that it actually runs, and that it finds the correct element. Commented Mar 29, 2013 at 18:27

1 Answer 1

1

Flash, and plugins in general, are a little bit fiddly. They don't behave quite like normal elements, and their functions don't behave quite like normal functions. For example, you can't save the element into a value and call a function from that. You also need to be careful because in some browsers the object is used and in others the embed is used.

The best way to call a function is to use swfobject (https://code.google.com/p/swfobject/) to abstract everything. Personally though, I use this (based on experience, maybe somebody can offer improvements):

HTML:

<object id="myplugin" ...>
    ...
    <embed name="myplugin" ... />
</object>

JavaScript:

var o1=document.myplugin;
if(o1&&!!o1.myFlashFunction){
    return document.myplugin.myFlashFunction(); // DO NOT USE o1 here. It will fail.
}
var o2=window.myplugin;
if(o2&&!!o2.myFlashFunction){
    return window.myplugin.myFlashFunction(); // DO NOT USE o2 here
}

The first case (document) is for most new browsers. For example, Chrome will find the embed object. The second (window) is for IE and finds the object (IE, at least old IE, ignores embed). I'm not 100% sure the second is needed, because IE might also work with document, so call that voodoo code. Also window.myplugin will give an array of all matching elements in Chrome, FireFox, etc. (but we expect those to already be taken care of)

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

2 Comments

I've done the console.log() on the piece making the call and it works fine. I'm hoping to not use SWFObject since I tried it already and it gave me layout problems.
@user2225288 so... are you still having a problem? Did you try the code I posted?

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.