Flash中生成动态Js方法,刷新页面
这也是js方法"eval"中的一种应用。利用flash生成js的方法,并让flash进行后续调用
同时,也可以通过eval,直接刷新整个html页面。
代码很简单,我就不直接说了,贴出来,大家可以直接看效果
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" fontFamily="Times New Roman"
fontSize="12" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.external.ExternalInterface;
private function init():void
{
//需要在js生成的代码
var str:String = "function testMethod(str)";
str = str + "{";
str = str + "alert('Flash传来:' + str);";
str = str + "return 'js返回:' + str;";
str = str + "}";
//利用eval函数生成
ExternalInterface.call("eval", str);
}
private function buttonClick():void
{
//调用刚刚生成的方法
var str:String = ExternalInterface.call("testMethod", valueText.text);
Alert.show(str);
}
private function fulshHtmlEvent():void
{
//刷新页面
ExternalInterface.call("eval", "location.reload();");
}
]]>
</mx:Script>
<mx:Button label="提交" id="subimtBtn" click="buttonClick()"/>
<mx:TextInput width="122" id="valueText"/>
<mx:Label text="Flash数据:"/>
<mx:Button label="刷新页面" id="flushBtn" click="fulshHtmlEvent()"/>
</mx:Application>