How to access query string arguments with AS3


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.external.*;
  4.  
  5. public class QueryString
  6. {
  7. private var _queryString:String;
  8. private var _all:String;
  9. private var _params:Object;
  10.  
  11. public function QueryString(url:String='')
  12. {
  13. readQueryString(url);
  14. }
  15. public function get getQueryString():String
  16. {
  17. return _queryString;
  18. }
  19. public function get url():String
  20. {
  21. return _all;
  22. }
  23. public function get parameters():Object
  24. {
  25. return _params;
  26. }
  27.  
  28. private function readQueryString(url:String=''):void
  29. {
  30. _params = new Object();
  31. try
  32. {
  33. _all = (url.length > 0) ? url : ExternalInterface.call("window.location.href.toString");
  34. _queryString = (url.length > 0) ? url.split("?")[1] : ExternalInterface.call("window.location.search.substring", 1);
  35. if(_queryString)
  36. {
  37. var allParams:Array = _queryString.split('&');
  38. //var length:uint = params.length;
  39.  
  40. for(var i:int=0, index=-1; i < allParams.length; i++)
  41. {
  42. var keyValuePair:String = allParams[i];
  43. if((index = keyValuePair.indexOf("=")) > 0)
  44. {
  45. var paramKey:String = keyValuePair.substring(0,index);
  46. var paramValue:String = keyValuePair.substring(index+1);
  47. _params[paramKey] = paramValue;
  48. }
  49. }
  50. }
  51. }
  52. catch(e:Error)
  53. {
  54. trace("Some error occured. ExternalInterface doesn't work in Standalone player.");
  55. }
  56. }
  57. }
  58. }
  59.  
  60. //
  61. // Call it like so:
  62. //
  63. var myPath:QueryString = new QueryString("http://www.studiosedition.com/?page=articles");
  64. trace(myPath.parameters.page);

URL: http://blog.studiosedition.com/2010/03/query-string-as3/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.