|
[ Exchanging Variables Between JavaScript & Flash ]
It may just be that I'm dense or uninformed, but one of the things that has plagued me down through the years is a reliable methodology for exchanging variables between JavaScript and Flash without mucking about with all the fun that are FSCommands. FSCommands, though powerful, have always been, well, a bit flaky to me platform-wise and more than a bit abstract. I finally know how now -- and it turns out to be simple.
Easily enough, you need to two things. First, import this library from the components: import flash.external.*;Second, learn this simple command structure: var greeting:String;
greeting = String(ExternalInterface.call("sayHello"));
var js_return = greeting;
What is it you're doing? That lovely, lovely ExternalInterface.call command is doing exactly what it says -- calling an external interface. In this case, a JavaScript function called 'sayHello' resident on the page the Flash is embedded in and returning a variable. Whoohoo!The JavaScript function isn't terribly complex in this case; it looks something like this: function sayHello() {
return "more";
}
Obviously, this is a terribly simplified function; all it does is in raw form is return the string variable 'more'. The ActionScript is wired to accept anything returned from sayHello as the string greeting, which is linked to the variable js_return. End analysis, js_return = "more". Big deal, right? But I picked it up from JavaScript -- now cool things can happen.In this specific case, the value for sayHello is generated dynamically -- it's mangled by the PHP driving the header for this site, and it returns part of the page name using $_SERVER['PHP_SELF'];. And yes, there's no reason why you couldn't do it by taking the index using JavaScript, but since the header is already PHP what the hell. The upshot of this is that now Flash is returned a variable from one of the five major sections of this site -- and sets state accordingly. It does this with this little bit of nonsense in Frame 5, after the preloader: js_where = js_return; gotoAndPlay(js_where);You just have to remember to set your frame names properly, and Voila!. All right, so I haven't cured cancer -- but that has really bothered me for a long time. All of this is, of course, available in Flash's Help under ExternalInterface.call [and, I'm sure, several places on the web] but it sure seemed hard to dig for and once I found it I had to get it out of my brain. You can also send a variable to JavaScript or [the reviled] ActiveX. If the JavaScript function was set up to accept a variable such as sayHello(name) and then do something with it like pop an alert, your call in ActionScript would look like this: greeting = String(ExternalInterface.call("sayHello", "Bob"));
which is exactly like calling from JavaScript as
sayHello('Bob');
If your JavaScript looks like this
function sayHello(name) {
alert(">> Hello " + name + ".");
return ">> Hi Flash.";
}
You get an alert box with ">> Hi Bob." and Flash responds with ">> Hi Flash." linked to greeting. Strangely enough, this is exactly what will happen if you download the simple example. [And no, I haven't wrapped it in the JS fix for IE. Sorry. Strangely enough, however, since JavaScript is activated and talking to the Flash, we don't get the box in IE6. Weird.]
|
