First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Nuno Mira | Website: http://www.nunomira.com/

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 we’ll 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

	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
			// something’s 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);	
		* 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);
		* 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, it’s displayed on the screen.


If not, you get an error:

image error

Here’s the complete example for download.

And here are all the files from the two examples for download.


SWFObject is one of Google’s hosted librarys, so you can link to it directly at:

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 don’t mix client side with server side code.

Can’t I append the variables to the SWF’s path as a query string instead of using FlashVars? It’s exactly the same query string that’s 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 don’t always come from the URL.


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-03-17
Rating: 1 Votes: 1
» 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.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.