How to use SWFAddress with ActionScript 2
10 August, 2010
*UPDATE* - 26 February, 2011
I finally had a good chance to sit down and look at the SWFAddress code and my examples, and there's good news! I've got everything sorted so SWFAddress works with the browser Back and Forward buttons.
I've updated the example files, which are linked below, so if you download them and get cracking on SWFAddress you should be all set.
Two things I found which are crucial to getting the browser buttons working: your SWF must be deployed to the webpage using SWFObject, and the SWFAddress JavaScript must be called before the SWFObject object is declared. I've updated the article and sample HTML below to reflect these new changes.
Today's article is a follow-up to an article we have done previously on SWFAddress.
SWFAddress allows us to have unique URLs for each page of a Flash based website. This is important for navigation and SEO.
So today we're going give a step-by-step run-down of how to implement SWFAddress with ActionScript 2. When we wrote the article previously, SWFAddress was only working with ActionScript 1 so that's what we based our article on. Now that it's been updated for ActionScript 2 and ActionScript 3, we thought it was time to have another look. We have also written an ActionScript 3 based article: How to use SWFAddress with ActionScript 3, if you're looking to use SWFAddress in an ActionScript 3 project. But for now, let's take a look at ActionScript 2.
For information of why it's a good idea to use SWFAddress with your Flash projects, you should check out our original article. We won't cover those reasons again here, but they still stand true today. Let's get started.
You can download the files you need to from the SWFAddress website, or from us directly:
- http://sourceforge.net/projects/swfaddress/files/swfaddress/SWFAddress%202.4/swfaddress-2.4.zip/download
- swfaddress-2.4.zip
Inside the ZIP archive, we're going to need the following files/directories:
- dist/as/2/com/asual/swfaddress/SWFAddress.as
- dist/as/2/com/asual/swfaddress/SWFAddressEvent.as
- dist/js/swfaddress.js
The .as files are ActionScript files we will need at compile time (when we export the SWF from Flash), and the .js file is JavaScript we will need at run time (on the website). The .as files aren't needed on the website, and the .js file isn't needed while we build the SWF.
So go ahead and copy the .js file to your webserver (SWFAddress only seems to work when run from a webserver, so while it can be developed without access to one, it will need one to work correctly), and the .as files to the directory where your Flash project is being/will be developed.
For our example file, here's what our basic timeline looks like:

You can see we just have three sections with some frame labels. Very simple. And we have three buttons on the stage. These buttons have the following instance names: redBTN, blueBTN, and yellowBTN.
We don't actually need a lot of code to get SWFAddress working for us in this instance, so we're going to list it all here now, and then run through how it works. The ActionScript we have on the first frame of our timeline:
stop();
blueBTN.onRelease = function() {
SWFAddress.setValue("blue");
}
blueBTN.onRollOver = function() {
SWFAddress.setStatus("blue");
}
blueBTN.onRollOut = function() {
SWFAddress.resetStatus();
}
redBTN.onRelease = function() {
SWFAddress.setValue("red");
}
redBTN.onRollOver = function() {
SWFAddress.setStatus("red");
}
redBTN.onRollOut = function() {
SWFAddress.resetStatus();
}
yellowBTN.onRelease = function() {
SWFAddress.setValue("yellow");
}
yellowBTN.onRollOver = function() {
SWFAddress.setStatus("yellow");
}
yellowBTN.onRollOut = function() {
SWFAddress.resetStatus();
}
SWFAddress.setStrict(false);
SWFAddress.onChange = function() {
var value = SWFAddress.getValue();
switch(value) {
case "blue":
gotoAndStop("blue_fl");
break;
case "red":
gotoAndStop("red_fl");
break;
case "yellow":
gotoAndStop("yellow_fl");
break;
}
}
It's important to note at this point, that we need to use different frame labels to the values we have appearing in the URL - SWFAddress doesn't work correctly if the frame labels and the URL values are the same.
So, we have a series of functions - three for each button. An onRollOver function for when the mouse rolls onto the button, an onRollOut function for when the mouse rolls off the button, and an onRelease function for when the button is clicked. We set up these three functions for each of the three buttons we have.
Each function runs a separate call to the SWFAddress class. We call setStatus and resetStatus on the rollover and rollout events, and for the onRelease event we call the setValue method. For the setStatus and setValue methods we pass in the value we want to show in the URL.
That's all we need for the button events, now we just have the code for what to do when the URL in the address bar changes.
The SWFAddress.onChange() function will fire each time the URL value is changed. So what we need to do is have Flash get the value we have assigned earlier, and use that to navigate. This means that on each button press, rather than the events being written to happen on the button press, we change the URL with SWFAddress, then SWFAddress recognises this change and reacts. This URL change reaction is important to get SWFAddress to work when a user enters a URL directly to head straight to a specific frame in a SWF.
In our example we set the value in the URL to the value variable, then run a switch statement based on what value is. If it's "blue", go to the blue_fl frame, if it's "red", go to the red_fl frame, and go to the yellow_fl frame if the value is "yellow". Simple.
One last thing we need to do it alter the SWFAddress ActionScript files, SWFAddress.as and SWFAddressEvent.as. When we get them in the SWFAddress package, they are buried deep at /com/asual/swfaddress. But, there's no need for those directories, so if you copy the as files into the same directory where your fla is, we need to make a change to the SWFAddress class.
Open both of the as files and remove the com.asual.swfaddress references.
For SWFAddress.as:
import SWFAddressEvent;
/**
* @author Rostislav Hristov
*/
class SWFAddress {
And for SWFAddressEvent.as:
import SWFAddress;
class SWFAddressEvent {
And that's about all there is to it.
- Name our frames on the timeline.
- Alter the referencing of our SWFAddress classes.
- Set up our button events to work with SWFAddress.
- Have SWFAddress recognise a URL change, and navigate the SWF file based on the value of the URL.
Provided we have the SWFAddress.as and SWFAddressEvent.as files in the same directory as the flash file, we're ready to export the SWF, upload it to our webserver and test it out.
For our webpage, we need to include the SWFAddress JavaScript file and the SWFObject JavaScript to get things working. If you aren't familiar with how to deploy your SWF to the webpage with SWFObject, you should check out our previous article on Embedding Flash with SWFObject so you can continue.
It's important that you include the SWFAddress JavaScript before you declare the SWFObject object.
Your JavaScript will look like:
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript" src="swfaddress/swfaddress.js?strict=false"></script>
<script type="text/javascript"
swfobject.registerObject("myFlash", "10.0.0");
</script>
Now, if you embed your SWF file in your webpage, you should end up with something similar to our example below. If you click the links in the list below as well, you will see the SWF navigating directly to a specific frame based on the URL.
As we finish up, it's interesting to note that SWFAddress can also control the title of your webpage as well, so even though the user is on the one webpage you can have different titles for each page of your SWF. This is important for SEO as well. We won't go into how to use the title methods of SWFAddress here as this article only helps you to get SWFAddress working in your Flash project, but we'll cover it in the future.
If you have any questions, let us know. As always you can download the sample files we have used to create this tutorial here: swfaddress_as2.zip.
ferrari_chris



Laura says:
2010-08-20 02:49:16
Thank you so much for this tutorial, it is the only one that has worked for me, but I still have problems with my site, sometimes the url changes but the swf doesn´t and I can't use the back buttons. Any help would be really appreciated.
__________
Chris (http://www.fcOnTheWeb.com) says:
2010-08-21 05:48:48
It's very hard to diagnose what your problem might be from here, but I would suggest putting some error checking into your FLA (such as a process writing some text to a text field) to see that the code in the SWF is executing when the URL is changed. If it's not changing, then I'd look at the JS that runs the SWFAddress.
Does this happen in more than one browser, and on other computers?
__________
Dan says:
2010-08-25 15:06:53
Hello! Thx a lot for this tutorial! I got just one problem.. when i press the previous page button, it leaves my website. If u can help me. Thx again :D
__________
Dan says:
2010-08-26 00:35:13
Nevermind.. i fixed it, thx!
__________
Abby says:
2010-09-30 22:00:13
This tutorial is great, but I'm having an issue. My buttons are buried within two movie clips. I figured out how to get them to change the url (by putting the script on the first layer of the innermost movieclip), but I can't get the a url change to affect the swf. No matter where I put the second half of the script, I can't get the url to change the swf. Any ideas on what I could be missing? Thanks!
__________
Chris (http://www.fcOnTheWeb.com) says:
2010-10-01 06:39:11
Hi Abby,
The first thing to do is make sure that the event in the SWF is being fired when the URL is changing.
I suggest you set up some sort of simple procedure (perhaps some text being entered into a text box) so you can be sure that when the URL is changing the SWFAddress.onChange function is actually picking it up.
If that's working OK, then we can look to the code that is being run but isn't doing what it's supposed to.
__________
Abby says:
2010-10-01 15:00:36
Thanks for getting back. It turned out it was my cache that kept me from solving this mostly. I've gotten it to work in Firefox, Chrome, and Opera, but IE is refusing to cooperate. Know of any IE work-arounds?
Here's the test site: http://www.pmgcommunications.com/test/pmg/portfolio.html
The icons on the bottom are the buttons. I'm only coding the first two for now.
__________
Abby says:
2010-10-01 19:59:39
I fixed it! I just had an outdated SWFObject javascript file. I figured it would be something silly like that.
For anyone else with a similar problem, here's a good tutorial for SWFObject 2.2: http://jtoolkit.com/flash/swfobject2/index.html
Thanks!
__________
Luka (http://www.a3a.co.rs) says:
2010-10-13 23:34:03
Hi, I started clicking on your "colour" buttons, and then tried to go forward/back - it didn't work. I have the same problem with my swfaddress Flash movie: it sends data to URL, it creates the History items, but on forward/back it does nothing, and the swfAddress:OnChange is actually working - so the browser isn't sending data BACK to flash. Do you know why?
__________
wiyono (http://www.quali-x.de) says:
2010-10-24 15:36:22
really great tutorial, thank you so much...
__________
Scott Humphrey says:
2010-11-09 13:19:51
Thanks for pointing out the frame label and the URl values must differ. I wasn't aware of that and was experiencing an annoying issue in IE. This solved it.
__________
Manuel says:
2011-12-31 18:29:47
Thanks for your tutorial.
I am interested to use SWFAddress with externals .swf files. From a menu in the main
.swf load (by pressing diferents buttons) several swf files that cover all the main swf.
It is possible ?. I am not be able to find one in the net.
Thanks in advance
__________
Carolyn says:
2012-01-11 20:44:17
Thank you for this tutorial. I finally got SWFaddress to work in FireFox and Chrome with no errors using your tutorial. I have SWFAddress 2.4, using AS2 and SWFObject 2.2 but links are not working in IE8 or IE7. When I try to test on my web server in IE, I get an object expected error and my browser crashes.
HTML:
<script type=\"text/javascript\" src=\"/scripts/swfobject/swfobject.js\"></script>
<!--<script type=\"text/javascript\" src=\"/customers/scripts/swfaddress.js\"></script>-->
<script type=\"text/javascript\" src=\"/customers/scripts/swfaddress.js?strict=false\"></script>
<script type=\"text/javascript\">
swfobject.registerObject(\"BNSF_Customer_Main-original-small-test\", \"8.0.0\");
</script>
<script type=\"text/javascript\">
var bandwidth_kbps_min = 250;
var swf = \"/customers/include/BNSF_Customer_Main-original-small-test.swf\";
var width = 550;
var height = 150;
var min_ver = 8;
var update = false; // offer an update to Flash?
</script>
ActionScript on 1st frame:
var counter:Number = 0;
//stop();
frame01BTN.onRelease = function() {
SWFAddress.setValue(\"cost\");
}
frame01BTN.onRollOver = function() {
SWFAddress.setStatus(\"cost\");
}
frame01BTN.onRollOut = function() {
SWFAddress.resetStatus();
}
frame02BTN.onRelease = function() {
SWFAddress.setValue(\"speed\");
}
frame02BTN.onRollOver = function() {
SWFAddress.setStatus(\"speed\");
}
frame02BTN.onRollOut = function() {
SWFAddress.resetStatus();
}
frame03BTN.onRelease = function() {
SWFAddress.setValue(\"reach\");
}
frame03BTN.onRollOver = function() {
SWFAddress.setStatus(\"reach\");
}
frame03BTN.onRollOut = function() {
SWFAddress.resetStatus();
}
frame04BTN.onRelease = function() {
SWFAddress.setValue(\"reliablity\");
}
frame04BTN.onRollOver = function() {
SWFAddress.setStatus(\"reliability\");
}
frame04BTN.onRollOut = function() {
SWFAddress.resetStatus();
}
frame05BTN.onRelease = function() {
SWFAddress.setValue(\"environment\");
}
frame05BTN.onRollOver = function() {
SWFAddress.setStatus(\"environment\");
}
frame05BTN.onRollOut = function() {
SWFAddress.resetStatus();
}
SWFAddress.setStrict(false);
SWFAddress.onChange = function() {
var value = SWFAddress.getValue();
switch(value) {
case \"cost\":
gotoAndPlay(\"Frame01\");
break;
case \"speed\":
gotoAndPlay(\"Frame02\");
break;
case \"reach\":
gotoAndPlay(\"Frame03\");
break;
case \"reliability\":
gotoAndPlay(\"Frame04\");
break;
case \"environment\":
gotoAndPlay(\"Frame05\");
break;
}
}
__________
Calin says:
2012-01-31 16:27:51
Hi, great tutorial. I'm having an issue.
How i can use the code for buttoms that are inside of a MC.
Add your comment on this article below:
Sorry, there's an error with your form entries. We really appreciate your comment, so please try again.
Form submitting now...