![](/img/trans.png)
[英]How can I call a Flex/Flash/Actionscript method from Java?
[英]Flex/Flash 4 ExternalInterface.call - trying to get a string from HTML to Actionscript
我需要从HTML获取一个字符串并将其放入Actionscript。
动作脚本:
import flash.external.ExternalInterface;
protected function getUserName():void{
var isAvailable:Boolean = ExternalInterface.available;
var findUserName:String = "findUserName";
if(isAvailable){
var foundUserName:String = ExternalInterface.call(findUserName).toString();
Alert.show(foundUserName);}}
javascript:
function findUserName() {
var label = document.getElementById("username-label");
if(label.value != ""){
alert("the name in the box is: " + label.value);
return label.value;}
else
return "nothing in the textbox";}}
JSP:
<%IUserSession userSession = SessionManager.getSession();%>
<logic:notEmpty name="userSession">
<logic:notEqual value="anonymous" name="userSession" property="userLoginId">
<td align="right" width="10%" >
<input id="username-label" type="text" value="<bean:write name="userSession" property="userLoginId"/>" />
</td>
</logic:notEqual>
</logic:notEmpty>
呈现的HTML:
<td align="right" width="10%">
<input id="username-label" type="text" value="a-valid-username" />
</td>
当javascript执行命中时
var label = document.getElementById("username-label");
返回null并崩溃,没有警报显示,没有错误消息显示。 我可以成功地做一个搜索Firefox的DOM查看器的“用户名标签”(的document.getElementById())
弹出的唯一警报框是动作脚本警报框,其内容为空白。
firfox 3.5 windows,容器是Tomcat。
请指教,并先谢谢您。
import flash.external.ExternalInterface;
public function getUserName():void{
var isAvailable:Boolean = ExternalInterface.available;
//var findUserName:String = "findUserName";
if(isAvailable){
ExternalInterface.call("findUserName");
}
}
现在创建一个按钮并调用此函数getUserName,然后尝试在JavaScript中放入警报消息,看看是否调用了该消息。
将方法设为“公开”,让我们知道您是否可以调用JavaScript方法名称。 查看我在ur函数中所做的更改
注意 :受保护和私有也将起作用,这仅用于测试。
更多更新:
function findUserName() {
// We are checking down initially itself.
if (!document.getElementById) return;
var label = document.getElementById(username-label);
alert(label);
if(label.value != ""){
alert("the name in the box is: " + label.value);
return label.value;}
else
return "nothing in the textbox";}}
将您的JavaScript函数更新到上面,让我知道。
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.