Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 17th, 2007, 04:45 AM
Member
 
Join Date: Aug 2007
Posts: 37
Default How do I access flash function using javascript?

Hi guys,

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

Thanks...
Reply
  #2  
Old August 17th, 2007, 05:09 PM
xNephilimx's Avatar
Expert
 
Join Date: Jun 2007
Location: Buenos Aires, Argentina
Age: 22
Posts: 162
Default

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
Quote:
Originally Posted by teenIce
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.
Quote:
Originally Posted by teenIce
Hi guys,

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

Thanks...
Reply
  #3  
Old October 3rd, 2007, 06:56 AM
Newbie
 
Join Date: Oct 2007
Posts: 1
Default

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...
Reply
  #4  
Old October 4th, 2007, 07:04 PM
xNephilimx's Avatar
Expert
 
Join Date: Jun 2007
Location: Buenos Aires, Argentina
Age: 22
Posts: 162
Default

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

Quote:
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.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...
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles