Javascript to flash communication
Asked Answered
P

6

8

I am trying to call a Actionscript function from javascript but I am having problems in Internet Explorer. I am using Swiff.remote in mootools 1.2.1 to call the actionscript function ie:

Swiff.remote(playSwf.toElement(), 'sendResult', result, plays, name);

This all works fine in FireFox, Safari and Opera but I'm getting an "unspecified" error in Internet Explorer 6 and 7. I have tried using the bog standard:

window['flash'].sendResult(result, plays, name);

To no avail.

Thanks for any help. Mark

Pilothouse answered 18/11, 2008 at 18:47 Comment(0)
D
5

I'm not familiar with the Swiff plugin, but you don't need a plugin to call flash functions from Javascript. It's even easier to do it natively.

From AS:

//1. calling javascript function from Flash.
ExternalInterface.call("sendData",tempStr);
// argument 1: javascript function, argument 2: data/variables to pass out.
//2. calling javascript function from Flash with recursion.
var returnValue:String = ExternalInterface.call("sendReturn",tempStr).toString();
//3. setting up a callback function for javascript
ExternalInterface.addCallback("callFlash",flashResponse);
// argument 1: function name called by javascript, argument 2: function on the Flash side.
// AS2 version looks like this : ExternalInterface.addCallback("callFlash",null,flashResponse);

From JS:

//1. javascript function as called from Flash.
function sendData(val){
    alert(val);
    document.flashForm.flashOutput.value = val;
}

//2. javascript function with recursion.
function sendReturn(val){
    var tempData = "Hello from JS";
    return tempData + ' :return';
}

//3. calling Flash function with javascript.
function sendToFlash(val){
    window['flash'].callFlash(val);
}
Disinfectant answered 18/11, 2008 at 19:41 Comment(4)
This is exactly what I have tried with: window['flash'].sendResult(result, plays, name); How are you referencing your flash object? window['flashname/flashid']Pensionary
Ah, of course. There must be a different way of grabbing that in IE. Maybe something like window.document.name... not sure on that.Disinfectant
Try window.document.getElementsByName('flashname').Disinfectant
It's not necessary to reference the flash with the window object, you can just reference by ID: work.arounds.org/calling-flash-functions-javascriptNerty
D
2

Ah, here is the answer to you problem.

<form>
    <input type="button" onclick="callExternalInterface(id)" value="Call ExternalInterface" />
</form>
<script>
function callExternalInterface(id) {
    thisMovie("externalInterfaceExample").callAS(id);
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}
</script>

SO if the client is Internet Explorer, you should be fetching the movie from the document object. :-)

Disinfectant answered 18/11, 2008 at 20:28 Comment(1)
What errors are you getting? This solutions seems sound, I wonder if there is some other factors causing your issues.Incapable
I
0

Wanted to post this answer, as this may be what's causing problems for others, obviously this is not causing your problem. Still looking into a solution for your issue.

From the MooTools Docs: http://mootools.net/docs/Utilities/Swiff Note:

The SWF file must be compiled with the ExternalInterface component. See the Adobe documentation on External Interface for more information.

Action Script 2.0

import flash.external.*;

Action Script 3.0

package com
{
   import flash.external.ExternalInterface;
   public class Main 
   {
   }
}
Incapable answered 20/11, 2008 at 16:33 Comment(0)
I
0

Maybe this can help you out, looks like a similar problem but using the swfobject.

http://blog.deconcept.com/swfobject/forum/discussion/1064/swfobject-21-problems-with-externalinterface-in-ie/

Good luck.

Incapable answered 20/11, 2008 at 16:42 Comment(0)
C
0

You can call it directly:

playSwf.remote('sendResult', result, plays, name)

Of course sendResult has to be registered with ExternalInterface.addCallback() in the AS code and the flash file has to fully loaded (otherwise all calls fail).

An example can be found in this github repository (fancyupload): The as3proj contains the AS source, the JS remote calls are in Swiff.Uploader.js.

Czarevitch answered 1/12, 2008 at 15:15 Comment(0)
R
0

If your code works in all browsers except Internet Explorer, it's a good bet that it's because the Flash Player for IE is an ActiveX plugin. I read somewhere that ActiveX communicates in .NET format and Flash's external API communicates in XML.

I'm also trying to learn Javascript-Flash communication on Internet Explorer, so I'll keep you folks posted on what I learn.

Rillet answered 15/6, 2009 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.