473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I access flash function using javascript?

37
Hi guys,

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

Thanks...
Aug 17 '07 #1
5 70576
xNephilimx
213 Expert 100+
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
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
213 Expert 100+
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
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
213 Expert 100+
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

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

Similar topics

1
by: | last post by:
Hello, I have a problem when I try to do this on a aspx page.. Is there anything wrong with this approach? This is the javascript function <Script Language='JavaScript'> function...
0
by: Bidarkota | last post by:
Can i access serverside selectbox using javascript in another page. if it is possible please help me with sample code. thanks in advance
1
by: lindy | last post by:
Hi All, I have a task need to update Sql server database whenever the user click "close" button in browser. It seemed I can't capture this event using asp.net. How can I access Sql server...
1
by: Debs | last post by:
Hi, I am running my website using Weblogic, so the path is something like http://<myip>:<port>/<servername>/ On JSP pages I can use <%=request.getContextPath()%to get the root path of the...
1
by: menakasrinivasagam | last post by:
set of News is stored in database.i retrieve each news fetch from database and display that news with flashing effect.fisrt news display,then that msg disappear and second msg flash and so on.give...
0
forsiam
by: forsiam | last post by:
I've seen the example in http://www.thescripts.com/forum/thread694359.html but I still have problem about javascript. (I use SWFObject) My Action Script import...
1
by: vartana | last post by:
how can i call a none static function using javascript? javascript:GetData() Public Sub GetData() Dim objNL As New NLEngine Dim dtNewsList As New Data.DataTable ...
1
by: visweswaran2830 | last post by:
Hi, I have a function in aspx.vb (code behind fucntion). Now I want to call this function using javascript... How can I call?
2
by: Marknut | last post by:
I have nested dynamic gridviews that I want to add “delete” and “edit” functionality to. The problem is that I can't use the addhandler event due to the postbacks (as I understand it anyway). So...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.