By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

How do I access flash function using javascript?

P: 37
Hi guys,

How do I access flash function using javascript?Does anyone have any reference or code?

Thanks...
Aug 17 '07 #1
Share this Question
Share on Google+
5 Replies


xNephilimx
Expert 100+
P: 213
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function loadB() {
  3.     var b:MovieClip = t.loadMovie("movieB.swf");
  4.     //assuming that the two movies have the same dimensions, position it in 0,0 so they are overlapping perfectly
  5.     b._x = b._y = 0;
  6.     //you can pass any parameters you vant, remember that b now is a movielcip within this function.
  7. }
  8.  
  9. btn.onRelease = loadB;
  10.  
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 ExternalInterface 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 allowScriptAcces 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="allowScriptAccess" value="always" />
then, in the embed:
<embed [...] allowScriptAccess="always" />

If you have SWFObject (which I recommend) you pass the parameter like this:
so.addParam("allowScriptAccess","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 ExternalInterface 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:

Expand|Select|Wrap|Line Numbers
  1. //Importing the class:
  2. import flash.external.ExternalInterface;
  3. //this class has 2 methods and they are used like this:
  4.  
  5. //the call method calls a javascript function
  6. somebtn.onRelease = function() {
  7.     var successful = ExternalInterface.call("jsFunction"[,"parameters"]);
  8. }
  9.  
  10. //the addCallback registers a function to be called from javascript, and this is what you wanted to do:
  11. var successful =  ExternalInterface.addCallback("methodName",instanceObject, realMethod);
  12.  
  13. function realMethod() {
  14.     //do something
  15. }
  16.  
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 ExternalInterface 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:
Expand|Select|Wrap|Line Numbers
  1. function getMovie(movieName) {
  2.     if (navigator.appName.indexOf("Microsoft") != -1) {
  3.         return window[movieName];
  4.     } else {
  5.         return document[movieName];
  6.     }
  7. }
  8.  
Or like this if you are using SWFObject:
Expand|Select|Wrap|Line Numbers
  1. function getMovie(movieName) {
  2.     return document.getElementById(movieName);
  3. }
  4.  
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:

Expand|Select|Wrap|Line Numbers
  1. function somefunc() {
  2.     var flash = getMovie('themoviename');
  3.     flash.methodName(parametersIfAny);
  4. }
  5.  
And that's it, as you can see, it's not too complicated. But if you read till here, let me tell you that I have an example that you can test and download to learn from it here: http://www.thenephilim.com.ar/tsdn/ei/
To download it go here http://www.thenephilim.com.ar/tsdn/e...lInterface.rar

Kind regards,
The_Nephilim
Yes.

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.
Hi guys,

How do I access flash function using javascript?Does anyone have any reference or code?

Thanks...
Aug 17 '07 #2

P: 1
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.getElementById("callBack");

flash.callMethod();
}

callBack is the id of the flash movie object that i have embedded on the page.

my flash actionscript is :

var successful:Boolean = ExternalInterface.addCallback("callMethod",this,FlashMethod);

function FlashMethod()
{
//home.t.text = "success";
getURL("http://www.google.com", _blank, "POST");
}

Please tell me if I am missing anything...
Thanks...
Oct 3 '07 #3

xNephilimx
Expert 100+
P: 213
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 allowScriptAccess parameter?

Best regards!
The_Nephilim

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.getElementById("callBack");

flash.callMethod();
}

callBack is the id of the flash movie object that i have embedded on the page.

my flash actionscript is :

var successful:Boolean = ExternalInterface.addCallback("callMethod",this,FlashMethod);

function FlashMethod()
{
//home.t.text = "success";
getURL("http://www.google.com", _blank, "POST");
}

Please tell me if I am missing anything...
Thanks...
Oct 4 '07 #4

P: 1
Hi,

I tried your code it works fine in IE but it does not work in FireFox. Can you tell me what to do in order to make it compatible with FireFox?
Sep 14 '09 #5

xNephilimx
Expert 100+
P: 213
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.
Sep 14 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.