2

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

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, it's the external (flash side) function which is not working - error message, myExternalMethod is not a function

4 Answers 4

1

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

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

2 Comments

Hi This calls a Javascript function by interacting with the flash, However I want to run a flash function by intereacting with the javacript
I have added another link for the other way around. The ExternalInterface can be used in both directions.
0

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

Comments

0

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/

8 Comments

Im getting getObjectById("myFlashID").myExternalMethod is not a function It seems to not be connecting to the flash function. Any suggestions?
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.
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
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?
I have this similar example nelsond8.com/?p=515#more, it is done in AS3, but AS2 code is almost identical.
|
0

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

1 Comment

Probably this was caused by not setting allowScriptAccess on your <embed> code

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.