问题
I need to obtain a string from HTML and put it into Actionscript.
the 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);}}
the 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";}}
the 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>
the rendered HTML:
<td align="right" width="10%">
<input id="username-label" type="text" value="a-valid-username" />
</td>
when the javascript execution hits
var label = document.getElementById("username-label");
a null is returned and crashes, no alert shows no error message is shown. I can successfully do a search firefox DOM Inspector by "username-label" (document.getElementById())
The only alert box that pops up is the action script alert box and the contents are blank.
firfox 3.5 windows, container is Tomcat.
Please advise, and thank you in advance.
回答1:
import flash.external.ExternalInterface;
public function getUserName():void{
var isAvailable:Boolean = ExternalInterface.available;
//var findUserName:String = "findUserName";
if(isAvailable){
ExternalInterface.call("findUserName");
}
}
Now create a button and call this function getUserName and now try to put a alert message in JavaScript and see if that calls.
Make the method Public and let us know whether you are able to call the JavaScript method name. See the changes i made in u r function
Note: Protected and Private will also work, this is just for testing.
More Update:
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";}}
Update your JavaScript function to above and let me know.
Thanks.
来源:https://stackoverflow.com/questions/2855599/flex-flash-4-externalinterface-call-trying-to-get-a-string-from-html-to-action