When developing applications associated with JavaScript should be possible to facilitate the work of a programmer
JSInterface library will be used
JSInterface library facilitates the interaction between Flash application and JavaScript environment. Using it, developer can do the same as in the JavaScript only by the ordinary means of ActionScript. In contrast to ExternalInterface, it provides a standard interface to access objects, their properties and methods.
It is possible to create JavaScript objects as usual ActionScript objects and use them in the ActionScript code - to change the value of their properties and to call their methods:
import aw.external.jsinterface.JSDynamic;
import aw.external.jsinterface.objects.JSArray;
var javaScriptArray:JSArray = new JSArray();
javaScriptArray[0] = true;
trace(javaScriptArray.length);
/*
1
*/
var javaScriptObject:JSDynamic = new JSDynamic('Object');
javaScriptObject.property1 = true;
javaScriptObject.property2 = true;
for(var name:String in javaScriptObject){
trace(name, "=", javaScriptObject[name]);
}
/*
property1 = true
property2 = true
*/
Using JSInterface you can create JavaScript functions
import aw.external.jsinterface.JSFunction;
var flashFriendlyAlert:Function = JSFunction.create(
"setTimeout(alert, 1, text);",
["text"]
);
flashFriendlyAlert("Simple Alert message.");
And assigns Flash function as the callback function for JavaScript events.
JSInterface has methods such as set/getCookie, set/getTitle, set/getHash, getLocation, for basic tasks
JSInterface.setTitle("My title");
trace("Title:", JSInterface.getTitle());
/*
Title: My title
*/
JSInterface.setCookie("tempCookie", "temp cookie value");
trace("Cookie:", JSInterface.getCookie("tempCookie"));
/*
Cookie: temp cookie value
*/
trace("Location:", JSInterface.getLocation());
/*
Location: file:///Z:/home/localhost/www/...
*/
JSInterface.setLocationHash("MyLocationHash/root/debug");
trace("Location hash:", JSInterface.getLocationHash());
/*
Location hash: #MyLocationHash/root/debug
*/