First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
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

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

image

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.

Note:

SWFObject is one of Google’s 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 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.

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
(10 being the highest)
» 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!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: doc_chin7
» Description:

The flash's horizontal road drive was done in CS4 and was upgraded to CS6. The car responds to right/ left key and flash will auto-generate the movement of roads from left-right or right-left based on random number. Have fun driving in this virtual environment and avoid other cars... :) If you are creative, a road driving game can be created from it.

Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
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: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.