FlashVars AS3 – getting values from the URL string

Getting values from the URL string in AS3 isn't very complicated. First you need to set up your html to pass these into the SWF using a javascript function:

  1.  
  2. <script language="javascript">
  3. function gup(name){
  4. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  5. var regexS = "[\\?&]"+name+"=([^&#]*)";
  6. var regex = new RegExp( regexS );
  7. var results = regex.exec( window.location.href );
  8. if( results == null ) {
  9. return "";
  10. } else {
  11. return results[1];
  12. }
  13. }
  14. </script>
  15.  

Place this inside your html.
Then if you are using the default HTML publishing you need to set up your SWF to take the variable. In this example I'm looking for a variable called 'transition'

  1.  
  2. <script language="javascript">
  3. if (AC_FL_RunContent == 0) {
  4. alert("This page requires AC_RunActiveContent.js.");
  5. } else {
  6. AC_FL_RunContent(
  7. 'codebase', 'http://download.macromedia.com...
  8. 'width', '100%',
  9. 'height', '100%',
  10. 'src', 'main',
  11. 'quality', 'high',
  12. 'pluginspage', 'http://www.macromedia.c...
  13. 'align', 'middle',
  14. 'play', 'true',
  15. 'loop', 'true',
  16. 'scale', 'exactfit',
  17. 'wmode', 'window',
  18. 'devicefont', 'false',
  19. 'id', 'main',
  20. 'bgcolor', '#ffffff',
  21. 'name', 'main',
  22. 'menu', 'true',
  23. 'FlashVars', 'transition='+gup("transition"),
  24. 'allowFullScreen', 'true',
  25. 'allowScriptAccess','sameDomain',
  26. 'movie', 'main',
  27. 'salign', ''
  28. ); //end AC code
  29. }
  30. </script>
  31.  
  32. ...
  33. <param name="scale" value="exactfit" />
  34. <param name="bgcolor" value="#ffffff" />
  35. <param name="FlashVars" value="transition="+gup("transition") />
  36. <embed src="main.swf" quality="high" scale="exactfit" ... />
  37. ...
  38.  
  39.  

The important lines are:

'FlashVars', 'transition='+gup("transition"),
and
param name="FlashVars" value="transition="+gup("transition")

Here we are calling the javascript function to return the value that matches 'transition'. Then this gets passed to the SWF.

Inside the flash this can be accessed with:

  1.  
  2. var sTrans:String = String(stage.loaderInfo.parameters.transition)
  3.  

Then you can get variables from the URL string:

http://www.sitedaniel.com/index.html?transition=myvalue

3 Comments

  1. Daniel says:

    You can add more values to this string by adding an ampersand (&) between value pairs

    http://www.sitedaniel.com/index.html?transition=myvalue&colour=red

  2. codrin says:

    Helped me!
    Thanks!

  3. Michael Dunn says:

    Thanks
    I have been looking for something like this for a long time. Nice post and it worked the first time. Now all I have to do is create a link to point to my various movie file.

    Do you have this code for CS5, I will be upgrading soon.

    Thanks again

Leave a Reply