0

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.

2
  • I'm testing on my Macs apache server. not browsing to the page through the file system, I've also set the root of apache's folders as a trusted location in the Global Security Flash settings. Commented Feb 23, 2013 at 22:39
  • Are you sure you call the method on the actual Flash Player instance, and not for example on the element that contains the Flash? In other words, can you confirm that mySWF references the right thing when you call mySWF.myExternalMethod()? Commented Feb 23, 2013 at 23:38

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

I added this code renaming the id/name appropriately and now it works perfectly, thanks @danni! You are a star!

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.