Hi. In answer to your two questions, first things first.
Loading movies
You can load a movie A inside another movie B without loosing the movie A. The best way to do that is by loading your movie B inside a movieclip in movie A. This way:
Let's suppose you have a button (instance name "btn") that will make the loading and the target movie clip is named (instance named) "t" -for "target"-, write this in your main timeline:
[CODE=actionscri pt]
function loadB() {
var b:MovieClip = t.loadMovie("mo vieB.swf");
//assuming that the two movies have the same dimensions, position it in 0,0 so they are overlapping perfectly
b._x = b._y = 0;
//you can pass any parameters you vant, remember that b now is a movielcip within this function.
}
btn.onRelease = loadB;
[/CODE]
And that's it, pretty simple and very usefull, is the key to making big sites, since you can load each section of the site externally.
Calling ActionScript from JavaScript and vice cersa To access javascript functions from ActionScript or vice versa, cab be done with the ExternalInterfa ce class that comes bundled and ready to use with since Flash 8. I don't know how to use it in Adobe Flash CS3, but I don't think it's going to be any different.
Here's how:
First of all, in your html embed code you must set the allowScriptAcce s property to "always" (i've never tryid if "sameDomain " instead of "always" works too, well, at least it doesn't work in local view of the file).
In the plain html you must set it in two ways fisrt in a param element:
<param name="allowScri ptAccess" value="always" />
then, in the embed:
<embed [...] allowScriptAcce ss="always" />
If you have SWFObject (which I recommend) you pass the parameter like this:
so.addParam("al lowScriptAccess ","always") ;
Now the actionscript part:
The fisrt thing you want to do here is to import the corresponding class in order to use it. The ExternalInterfa ce class it's used directly from the class, you don't need (and it won't work if you do) to instantiate it, because all the methods are static (let's say, the whole class is aware of this methods), let's begin with examples of this fisrt part:
[CODE=actionscri pt]
//Importing the class:
import flash.external. ExternalInterfa ce;
//this class has 2 methods and they are used like this:
//the call method calls a javascript function
somebtn.onRelea se = function() {
var successful = ExternalInterfa ce.call("jsFunc tion"[,"parameters "]);
}
//the addCallback registers a function to be called from javascript, and this is what you wanted to do:
var successful = ExternalInterfa ce.addCallback( "methodName",in stanceObject, realMethod);
function realMethod() {
//do something
}
[/CODE]
With the call method you call a javascipt function as is, in your javascript you do not need to do anything special., just write a function with the corresponding name.
But when in comes to addCallback there are some other things to have in mind.
The instance parameter for most of the cases just use this.
The methodName is the name that will be used in JavaScript.
And the realMethod is the call to the real function that will be in the flash movie.
You always must first add the ExternalInterfa ce callbacks, and then (below) declare the functions.
Now in your js you need first to get the embed element of your flash movie like this:
[CODE=javascript]
function getMovie(movieN ame) {
if (navigator.appN ame.indexOf("Mi crosoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
}
[/CODE]
Or like this if you are using SWFObject:
[CODE=javascript]
function getMovie(movieN ame) {
return document.getEle mentById(movieN ame);
}
[/CODE]
Now we set the function sin javascript that will call the ones in the flash movie. All you got to do now is: get the flash movie and call the function like a method of the movie. Like this:
[CODE=javascript]
function somefunc() {
var flash = getMovie('themo viename');
flash.methodNam e(parametersIfA ny);
}
[/CODE]
It works..Thanks Kestrel. But I want your opinion in my scenario. I had two flash file (A and B) . Let say, A supposedly call B and pass certain variables, is it possible I can load B in the same page with A and never unload A.
Thank you in advance.
Originally posted by teenIce
Hi guys,
How do I access flash function using javascript?Does anyone have any reference or code?
Hi, jwalants!
The code seems to be ok, please post the HTML code you used to embed the movie.
I also need to know what method did you use? SWFObject or pure HTML? And, either case, did you put the allowScriptAcce ss parameter?
Best regards!
The_Nephilim
Originally posted by jwalants
Hi, I have tried to do same as you have posted. But it doesn't work for me.
my Javascript snippet is :
function Method()
{
var flash = document.getEle mentById("callB ack");
flash.callMethod();
}
callBack is the id of the flash movie object that i have embedded on the page.
my flash actionscript is :
var successful:Bool ean = ExternalInterfa ce.addCallback( "callMethod",this,FlashMet hod);
This is an old one! haha.
Remember that you should call the javascript snippet only when the page has fully loaded, window.onload = callback_fn; in pure javascript or $(callback_fn); in jQuery (recommended, since this one executes when just the DOM is fully loaded, the other one waits until everything has loaded).
I didn't tried this lately, but this could be the answer, If it still doesn't work, I suggest you to install Firebug JS debugger so you can provide me with whatever error firebug returns. I've been away from AS and Flash for quite a long time, I'm a php and js expert now. So, even though I don't think this is an AS error, I won't be able to further assist you with AS.
Last edited by xNephilimx; Sep 14 '09, 02:13 PM.
Reason: typossssss
Comment