adamg2000
adamg2000

Reputation: 543

Trigger a flash objects function embedded via swfobject's embedSWF?

I have looked through every resource I could find to create a streamlined way of doing this but I get one of the fundamental errors each time I try. I simply cannot get it to work!

Here is my flash test actionscript from the first keyframe of my movie:

import flash.external.*; //THIS...

System.security.allowDomain("http://localhost", "*"); //...AND THIS ALSO REQUIRED!

if (ExternalInterface.available) {
    trace("ExternalInterface= " + ExternalInterface.available);
    flash.external.ExternalInterface.addCallback("myExternalMethod", null, myFunction);
}

function myFunction() {
    gotoAndStop(2);
}

And here is my javascript/little bit of jQuery:

<script type="text/javascript">
    var flashvars = {};
    var params = {
        wmode: "transparent", 
        allowscriptaccess: "always"
    };

    var attributes = { //The addition of these attributes make it work!
        id: "number-input",
        name: "number-input"
    };

    var embedHandler = function (e){
        mySWF = e.ref;
    };

    swfobject.embedSWF("images/flash-form.swf", "number-input", "800", "320", "9.0.0", "expressInstall.swf", flashvars, params, attributes, embedHandler);

    function executeFlash() {
        getObjectById("number-input").myExternalMethod(); 
    }

    function getObjectById(objectIdStr) {
        var r = null;
        var o = document.getElementById(objectIdStr);
        if (o && o.nodeName == "OBJECT") {
            if (typeof o.SetVariable != undefined) {
                r = o;
            } else {
                var n = o.getElementsByTagName(OBJECT)[0];
                if (n) {
                    r = n;
                }
            }
        }
        return r;
    }

    $(function() {
        $('#main-button-big').click(function(event){
            event.preventDefault();
            executeFlash();
        });

    });
</script>

The flash seems to embed successfully, if I do something silly like...

$(mySWF).hide();

...it hides, so I'm sure the script can see the object.

No matter what methods I've tried to do I always (testing in firefox) get an error like:

Error: TypeError: mySWF.myExternalMethod is not a function.

In safari:

TypeError: 'undefined' is not a function (evaluating 'mySWF.myExternalMethod()')

If I try the jQuery version of getElementById (circumnavigating the embedHandler) I get:

Error: TypeError: $(...).myExternalMethod is not a function. or TypeError: 'undefined' is not a function (evaluating '$('#number-plate-input').myExternalMethod()')

I need to get this project finished rapidly and am at the end of my tether. Any help would be very gratefully received.

UPDATED: Please note that the embedHandler is no longer needed, nor used to trigger any event of any kind. It is a useful piece of code though, so I've opted to leave it in.

Upvotes: 0

Views: 3375

Answers (1)

danii
danii

Reputation: 5693

Change the attributes variable in your javascript from an empty object to:

var attributes = {
  id: "flash_movie",
  name: "flash_movie"
};

And then you should be able to call the exposed function using:

document.getElementById("flash_movie").myExternalMethod();

Upvotes: 1

Related Questions