
Passing URL variables into Flash using FlashVars and SWFObject - Page 4
Another scenario:
A more interesting scenario than just displaying three strings in text fields is loading an image.
The code is pretty much the same but well have one variable only: img.
This is the different JavaScript:
if (swfobject.getQueryParamValue("img"))
{
flashvars.img = swfobject.getQueryParamValue("img");
}
And this is the new Document Class: MainImg.as
package
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
public class MainImg extends Sprite
{
// Loader object to load the image
private var loader:Loader;
public function MainImg():void
{
// make sure the stage property exists
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
var img:String;
// make sure the variable exists before trying to use it
if (stage.loaderInfo.parameters.img != undefined)
{
img = stage.loaderInfo.parameters.img;
// load the image using the variable received
loadImage(img);
}
else
{
// somethings wrong
status_txt.text = "The variable is missing";
}
}
/**
* try to load the image
* @param img
*/
private function loadImage(img:String):void
{
status_txt.text = "Loading the image...";
loader = new Loader();
var request:URLRequest = new URLRequest(img);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, error);
loader.load(request);
}
/**
* when the image loads, add it to the display list
* @param e
*/
private function complete(e:Event):void
{
status_txt.text = "Image loaded";
var bm:Bitmap = Bitmap(loader.content);
addChild(bm);
}
/**
* there was an error loading the image
* the received variable isn't correct
* @param e
*/
private function error(e:IOErrorEvent):void
{
status_txt.text = "Image not found";
}
}
}
First you receive the image name which comes from the URL via FlashVars. Then you try to load it. If the image exists, its displayed on the screen.
If not, you get an error:
Heres the complete example for download.
And here are all the files from the two examples for download.
Note:
SWFObject is one of Googles hosted librarys, so you can link to it directly at:
http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js
by using this code:
Some questions:
Grabbing parameters from the URL can also be done with a server side scripting language such as PHP. Why use SWFObject?
Because a server side scripting language is not always available, and as you should be using SWFObject already, this is the natural choice. And in the end, you get cleaner code, where you dont mix client side with server side code.
Cant I append the variables to the SWFs path as a query string instead of using FlashVars? Its exactly the same query string thats in the URL...
Yes, but the code is not so clear:
swfobject.embedSWF("url-variables.swf?firstname=john&lastname=doe&number=55", "flashContent","550", "400");
This string is both harder to read and to build.
Besides, variables passed via FlashVars dont always come from the URL.
Conclusion:
You should now be able to pass variables from the HTML page to the SWF via FlashVars, using SWFObject, even when this variables come from the URL.
You should be able to access and use these variables in the main SWF file or an external one.
» Level Intermediate |
Added: 2011-02-28 Rating: 1 Votes: 7 |
» Author |
Nuno Mira has been a Flash Developer for 9 years. He loves teaching, and learning. When he isn't coding he may be surfing or snowboarding. |
» Download |
Download the files used in this tutorial. |
Download (572 kb) |
» Forums |
More help? Search our boards for quick answers! |