as3 How to get the exact website URL? which only contain e.g:

7 posts (showing 1-7)

Market Level 6Community Level 4
344 posts

My sponsor have a really tough branding

he wants me to get the exact website URL? which only contain e.g:

Im using  stage.loaderInfo.url, but this will get the whole site like.

any one knows it? it will very well appreciated thanks

posted 2014-12-18T00:38:28-08:00 | edited 2014-12-18T00:39:49-08:00

Market Level 1Community Level 1
48 posts

Here ya go

var url:String = Lib.current.stage.loaderInfo.url;
var urlStart:Int = url.indexOf("://") + 3;
var urlEnd:Int = url.indexOf("/", urlStart);
var domain:String = url.substring(urlStart, urlEnd);
var LastDot:Int = domain.lastIndexOf(".") - 1;
var domEnd:Int = domain.lastIndexOf(".", LastDot) + 1;
domain = domain.substring(domEnd, domain.length);[/code]

Remove "lib.current" and it should be good! (It's for Haxe + openFL ATM)
The var named 'domain' will be something like "".

Edit: Oh and I think integers are called "int" not "Int" in AS3, so you might have to change that too

posted 2014-12-18T02:44:53-08:00 | edited 2014-12-18T02:46:32-08:00

Market Level 6Community Level 4
344 posts

great thanks men! 

posted 2014-12-18T02:45:37-08:00

Market Level 0Community Level 0
9 posts

Good example, though I'd add more safeguards here:

1. URL's can have port numbers, e. g. It's very unlikely to meet this notation in real life, but sometimes it happens, so better be ready.

2. It's good to have method to "parse domain from SWF URL", but way better is to have method to "parse domain from URL". You never know when you'll need to parse URL's other than SWF paths, so it's a good programming practice to write methods as universlal as possible, covering all cases.

Here is my modification of Zarmeck's code:

var domain:String = stage.loaderInfo.url;
if(domain.indexOf("://") > 0){
    domain = domain.substr(domain.indexOf("://") + 3);
if(domain.indexOf("/") > 0){
    domain = domain.substr(0, domain.indexOf("/"));
if(domain.indexOf(":") > 0){
    domain = domain.substr(0, domain.indexOf(":"));
if(domain.match(/\./g).length > 1){
    var dom_dot:int = domain.lastIndexOf(".");
    domain = domain.substr(domain.substr(0, dom_dot).lastIndexOf(".") + 1);


posted 2015-01-11T12:52:38-08:00

Market Level 6Community Level 3
334 posts

maybe something a bit simpler:

var D=stage.loaderInfo.url.split('/')[2].split(':')[0];
posted 2015-01-12T15:34:30-08:00

Market Level 0Community Level 0
1 posts

var url_full:String = stage.loaderInfo.url;

var ini:int = (url_full.indexOf("http")+1)?url_full.indexOf("//")+3:0;

var domain:String = url_full.substring(ini, url_full.indexOf("/", ini));

posted 2015-09-30T23:02:18-07:00

Market Level 7Community Level 5
564 posts

I don't know if any of the above code snippets would deal with non .com domains, such as

This is what I use, which seems to work well:


//get the domain from the url (eg. becomes
var url = stage.loaderInfo.url;
var exp:RegExp = /(?!http:\/\/)([a-z0-9\-]+)\.([a-z]+|[a-z]{2}\.[a-z]+)([\/])/ig;
domain = url.match(exp)[0];


posted 2015-10-02T06:48:46-07:00