Given that there is a global javascript variable on my web page named myVar, how can I access the value of the variable myVar from within my flash movie using javascript?
鉴于我的网页上有一个名为myVar的全局javascript变量,如何使用javascript从我的flash影片中访问变量myVar的值?
I see plenty of examples of using external interface in order to execute javascript from actionscript, but I am unable to find examples of returning values back into the flash movie using actionscript.
我看到很多使用外部接口的例子,以便从actionscript执行javascript,但是我无法找到使用actionscript将值返回到flash电影中的示例。
Thanks in advance. I hope my question is clear enough.
提前致谢。我希望我的问题很清楚。
4 个解决方案
#1
ExternalInterface works by allowing JavaScript to invoke an ActionScript function in the movie, and vice-versa. You can optionally receive a return value back from the invoked function. Here's a very simple example:
ExternalInterface允许JavaScript在电影中调用ActionScript函数,反之亦然。您可以选择从调用的函数接收返回值。这是一个非常简单的例子:
JavaScript:
<script language="JavaScript">
function getMyVar()
{
return myVar;
}
</script>
Flash/AS:
import flash.external.ExternalInterface;
var result:string = ExternalInterface.call("getMyVar");
#2
You can also provide an anonymous function that returns your global variable value to the ExternalInterface.call
method as follow:
您还可以提供一个匿名函数,将您的全局变量值返回给ExternalInterface.call方法,如下所示:
ExternalInterface.call("function(){ return myGlobalVariable; }");
#3
I've noticed Rex M's answer is a bit incomplete.
我注意到Rex M的答案有点不完整。
He was right about using...
他正确使用......
import flash.external.ExternalInterface;
var result:string = ExternalInterface.call("getMyVar");
Then in your javascript you can use
然后在您的JavaScript中,您可以使用
<script language="JavaScript">
function getMyVar() {
return myVar;
}
</script>
However in order to use this, the flash movie must be in an html accessed over http. Not using file://
但是为了使用它,flash影片必须是通过http访问的html。不使用file://
Here is a tutorial for communicating from actionscript to javascript and vice versa. http://www.youtube.com/watch?v=_1a6CPPG-Og&feature=plcp
这是一个从actionscript到javascript进行通信的教程,反之亦然。 http://www.youtube.com/watch?v=_1a6CPPG-Og&feature=plcp
#4
You can also do this:
你也可以这样做:
ExternalInterface.call("eval","getVar=function(obj){return obj}");
var yourVar:String = ExternalInterface.call("eval","getVar(JSvar)");
#1
ExternalInterface works by allowing JavaScript to invoke an ActionScript function in the movie, and vice-versa. You can optionally receive a return value back from the invoked function. Here's a very simple example:
ExternalInterface允许JavaScript在电影中调用ActionScript函数,反之亦然。您可以选择从调用的函数接收返回值。这是一个非常简单的例子:
JavaScript:
<script language="JavaScript">
function getMyVar()
{
return myVar;
}
</script>
Flash/AS:
import flash.external.ExternalInterface;
var result:string = ExternalInterface.call("getMyVar");
#2
You can also provide an anonymous function that returns your global variable value to the ExternalInterface.call
method as follow:
您还可以提供一个匿名函数,将您的全局变量值返回给ExternalInterface.call方法,如下所示:
ExternalInterface.call("function(){ return myGlobalVariable; }");
#3
I've noticed Rex M's answer is a bit incomplete.
我注意到Rex M的答案有点不完整。
He was right about using...
他正确使用......
import flash.external.ExternalInterface;
var result:string = ExternalInterface.call("getMyVar");
Then in your javascript you can use
然后在您的JavaScript中,您可以使用
<script language="JavaScript">
function getMyVar() {
return myVar;
}
</script>
However in order to use this, the flash movie must be in an html accessed over http. Not using file://
但是为了使用它,flash影片必须是通过http访问的html。不使用file://
Here is a tutorial for communicating from actionscript to javascript and vice versa. http://www.youtube.com/watch?v=_1a6CPPG-Og&feature=plcp
这是一个从actionscript到javascript进行通信的教程,反之亦然。 http://www.youtube.com/watch?v=_1a6CPPG-Og&feature=plcp
#4
You can also do this:
你也可以这样做:
ExternalInterface.call("eval","getVar=function(obj){return obj}");
var yourVar:String = ExternalInterface.call("eval","getVar(JSvar)");