How to read javascript global variable from actionscript

Given that my webpage named myVar has a global javascript variable, how can I access the value of the myVar variable from my flash movie using javascript?

I see many examples of using the external interface to execute javascript from ActionScript, but I can not find examples of returning values ​​back to a flash movie using actionscript.

Thanks in advance. I hope my question is clear enough.

+4
source share
4 answers

ExternalInterface works by letting JavaScript call the ActionScript function in the movie and vice versa. You can optionally get the return value back from the called function. Here is a very simple example:

JavaScript:

<script language="JavaScript"> function getMyVar() { return myVar; } </script> 

Flash / AS:

 import flash.external.ExternalInterface; var result:string = ExternalInterface.call("getMyVar"); 
+6
source

You can also provide an anonymous function that returns the value of a global variable to the ExternalInterface.call method as follows:

 ExternalInterface.call("function(){ return myGlobalVariable; }"); 
+2
source

I noticed that the Rex M answer is a bit incomplete.

He was right about using ...

 import flash.external.ExternalInterface; var result:string = ExternalInterface.call("getMyVar"); 

Then in your javascript you can use

 <script language="JavaScript"> function getMyVar() { return myVar; } </script> 

However, to use this, the flash movie must be in html accessible via http. Do not use file: //

Here is a tutorial for communicating with actionscript in javascript and vice versa. http://www.youtube.com/watch?v=_1a6CPPG-Og&feature=plcp

+1
source

You can also do this:

 ExternalInterface.call("eval","getVar=function(obj){return obj}"); var yourVar:String = ExternalInterface.call("eval","getVar(JSvar)"); 
0
source

All Articles