I have a flash (AS2.0) application with a function that i need to trigger from a html form link The flash function only runs a gotoAndPlay('label name'); So my HTML is
<a href="" id="flashTrigger" />

and my flash function is

  
function myFunction(){
gotoAndPlay("myLabel");
}

Anyone know either how I can fire the flash function from the html link tag, OR run a "gotoAndPlay" from a Javascript function

Iv looked around and only seem to find how to fire a javascript function from flash

Cheers

Andy

Here is the code I have so far - Im prob doing something stupid
Flash:

ExternalInterface.addCallback( "myExternalMethod", this, myFunction );

function myFunction(){
gotoAndPlay("fade");
}

Javascript

function executeFlash()
{
  getObjectById("myFlashID").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() {

    $('#WhatDoesmean').click(function(){
        executeFlash();
    });

});

I have set myFlashID to the id of:
initial 'Object tag' and IE only 'embed tag'

EDIT: At the moment I am targeting the flash object fine Its the external (flash side) function which is not working - error message, myExternalMethod is not a function

share|improve this question

4 Answers

You can use the external interface for this:

There is a working example at this location:

Flash to JS: http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001655.html

JS to Flash: http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001653.html

Hope this helps

Cheers

share|improve this answer
Hi This calls a Javascript function by interacting with the flash, However I want to run a flash function by intereacting with the javacript – atmd May 24 '11 at 11:23
I have added another link for the other way around. The ExternalInterface can be used in both directions. – DennisJaamann May 24 '11 at 11:31

I used this a few years ago to capture the flash object:

var flashObj = getFlash("Flash_Name");

function getFlash(movieName)
{
if (window.document[movieName])
{
    return (window.document[movieName]);
}
if (navigator.appName.indexOf("Microsoft Internet")==-1)
{
    if (document.embeds && document.embeds[movieName])
    {
        return (document.embeds[movieName]);
    }
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
    return (document.getElementById(movieName));
}
}

Then a flashObj.gotoAndPlay(); should work

share|improve this answer

In your flash file add a callback:

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

function myFunction()
{
    gotoAndPlay("myLabel");
}

In your javascript:

function executeFlash()
{
 //**updated**
  alert('JS call works fine!');
  getObjectById("myFlashID").myExternalMethod(); // myFlashID = your SWF object ID
}

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

In your HTML (give your SWF object embedded in your HTML an id = myFlashID):

<a href="" id="flashTrigger" onclick="executeFlash()" />

Here is where you find the documentation for ExternalInterface http://flash-reference.icod.de/

share|improve this answer
Im getting getObjectById("myFlashID").myExternalMethod is not a function It seems to not be connecting to the flash function. Any suggestions? – atmd May 24 '11 at 12:07
Are you placing getObjectById("myFlashID").myExternalMethod inside Flash or in your javaScript? And myFlashID should be the ID name you gave to your swf object when you embedded it into your HTML page. – nelsond8 May 24 '11 at 12:23
Im putting the getObject etc in the javascript - Iv edited the question to show my full code with your solution (which I think is the closest Iv seen) - Very greatfull for your help on this – atmd May 24 '11 at 12:55
I updated my code by adding a trace and Alert, so we can pinpoint what is not working. Have a look at it and let me know! Also are you using swfObject? – nelsond8 May 24 '11 at 13:11
1  
I have this similar example nelsond8.com/?p=515#more, it is done in AS3, but AS2 code is almost identical. – nelsond8 May 24 '11 at 16:36
show 3 more comments
up vote 0 down vote accepted

I have managed to crack the problem by using SWFObject

Accessign the flash objects method was easy through SWFObject.js, however it just wasnt working without it. Not sure why though.

All the above suggestions work with SWFObject.js, but none seemed to work without it

Cheers for everyones suggestions

Andy

share|improve this answer
Probably this was caused by not setting allowScriptAccess on your <embed> code – DennisJaamann Sep 27 '12 at 10:29

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.