jBookmarker and Flash
I don't know Flash and nor I intend to learn it in the near future, so I went to Aurelian for help, as he's the Flash guru around here. The thing is that he doesn't know Javascript and nor does he intend to learn it in the near future. It seemed like a dead end, but, fortunately for us, there are other people out there that like both of these technologies and share their knowledge with the world. So we've found everything we needed on this page.
The Javascript
This was my part. The mechanism behind it is very similar to the one I've described in the jBookmarker and Ajax section, the only difference is that the key from the hash is sent to the Flash movie and everything happens there. Here's a quick walkthrough:
First of all, we need a function to select the flash object:
function getFlashMovie( movieName ) {
var isIE = navigator.appName.indexOf( "Microsoft" ) != -1;
return ( isIE ) ? window[movieName] : document[movieName];
}
Then, we need the callback function:
initApp = function() {
var selected = jBookmarker.get( 'selected' );
if ( !selected ) {
selected = 'hello';
}
getFlashMovie( 'flash' ).sendTextToFlash( selected );
}
Don't worry about the "sendTextToFlash" function as is documented below, in the Action Script part. Next we'll need a function that saves the current pageID into the hash. It will be invoked from inside the Flash embed when the user clicks a button.
getTextFromFlash = function( page ) {
jBookmarker.set( 'selected', page );
}
Last thing we have to do is adding the listeners:
window.onload = function() {
initApp();
jBookmarker.addListener( initApp );
}
The Action Script - written by Aurelian
Using jBookmarker with Flash isn't that big of a deal.. I know it's not the best code in town, but bear with me...
To communicate with JavaScript, you first have to import the ExternalInterface class:
import flash.external.ExternalInterface;
Then to register an actionscript method from js I'm using this code which registers the 'changePage' method so it can be called from the 'sendTextToFlash' js method:
ExternalInterface.addCallback("sendTextToFlash", this, changePage);
The 'init' function assigns a global variable to each button and also defines a function for the onRelease event of each button
function init() {
var this_mc:MovieClip;
for (i=1;i<=3;i++) {
this_mc = eval("buton"+i+"_mc");
switch(i) {
case 1:
this_mc.var_glob="hello";
break;
case 2:
this_mc.var_glob="about_us";
break;
case 3:
this_mc.var_glob="contact";
break;
}
this_mc.onRelease = function() {
clickSend(this);
}
}
}
The clickSend method sends the global variable of each movieclip(button) to js by calling the 'getTextFromFlash' method using the ExternalInterface.call method
function clickSend(this_mc:MovieClip):Void {
var jsArgument:String = String(this_mc.var_glob);
var result:Object = ExternalInterface.call("getTextFromFlash", jsArgument);
changePage(jsArgument);
}
The 'changePage' method just changes the content of the text_str depending on the name of the button(the 'page' parameter) -- i know i should've used ids, but if you got that already, then you know how to modify this also.
function changePage( page:String ) {
var text_str:String;
switch(page) {
case "hello":
text_str = "HELLO \n\r ***long text***";
break;
case "about_us":
text_str = "ABOUT US \n\r ***long text***";
break;
case "contact":
text_str = "CONTACT \n\r ***long text***";
break;
}
received_txt.html = true;
received_txt.htmlText =text_str;
}
Of course, changing to another page could mean loading some other swf's or starting some sort of animations, just try to modify each case in the switch for what you need.
Starting the script by calling the init method;
init();
I know you will have questions, so don't hesitate contacting me(not Tudor). I might even answer (: