Get URL and Read URL Parameters in JavaScript, HaXe and AS3

Datetime:2016-08-23 02:54:36          Topic: JavaScript           Share

Find the URL of the embedding page of a swf is a very basic way for domain locking flash games. Similar things can be done for JavaScript based online games. To get the URL, in JavaScript you can use

var myURL=document.URL;

In HaXe (targeting JavaScript or HTML5), you can use

var myURL:String=js.Browser.window.document;

In AS3, to find the path url of the swf, you can use

root.loaderInfo.loaderURL;
//or
root.loaderInfo.url;

and for finding the path url of the embedding page, you can use

ExternalInterface.call("window.location.href");

Besides, it's common to see url parameters, for example: " http://mysite.com/index.html?param1=1234&param2=somestr&param2=someotherstr " To read the parameters, in the above example, that is "1234", "somestr" and "someotherstr", in JavaScript, you can use the snippet provided by http://stackoverflow.com/a/979995/1100006 or the function given at http://css-tricks.com/snippets/javascript/get-url-variables

In HaXe, you can use the following HaXe function

//translated from http://css-tricks.com/snippets/javascript/get-url-variables/
function getQueryVariable(variable):String
{
       var query:String = js.Browser.window.location.search.substring(1);
       var vars:Array

  = query.split("&");
       for ( i in 0...vars.length) {
	   var pair:Array
 
   = vars[i].split("=");
	   if(pair[0] == variable){return pair[1];}
       }
       return("null");
}

 

In AS3, if the parameters are given in the path of the swf' url, for example, " http://mysite.com/myswf.swf?param1=1234&param2=somestr&param2=someotherstr ", or if the parameters are declared in flashvars, then you can simply use " root.loaderInfo.parameters " object to access all the parameters, for example,

var myStr:String = root.loaderInfo.parameters.param1;

However, to read parameters of the embedding page' url, you still need the help of JavaScript, see the following pages for an example:

http://snipplr.com/view/44852/how-to-access-query-string-arguments-with-as3/




About List