views:

332

answers:

2

I know how to get the URL of the page, but how can I extract simply the domain and the domain alone?

It must return the same value with or without www, and it must also return the same value regardless of file, with or without trailing slash, etc.

So www.domain.com would return domain.com, and domain.com/index.php would return the same as well.

Is this possible?

If so, is there a way to do it without calling ExternalInterface.call('window.location.href.toString')?

Thank you for the help!

A: 

In Flex use

Application.application.url

But in straight Flash you need to do it differently

http://renaun.com/blog/2008/10/16/264/

Then of course you can hack up the result as you need to, since it's a string.

Yar
Can't do it in flex, must be flash.How do I 'hack up the result'?
Cyclone
String manipulation in AS3: http://www.wuup.co.uk/as3-basics-string-manipulation-in-actionscript-3/There are probably better sites, too.
Yar
+1  A: 

You can use the loaderInfo class, and then trim it down with a regular expression.

Like this. This trace of found[0] will return the domain down to the .com.

package{

import flash.display.LoaderInfo
import flash.display.MovieClip


public class sample extends MovieClip {
    public var urlStr:String;

    public function sample (){
        getLocation(this.loaderInfo.url);

    }
    public function getLocation(urlStr:String){
        var urlPattern:RegExp = new RegExp("http://(www|).*?\.(com|org|net)","i");
        var found:Object =  urlPattern.exec(urlStr);
            trace(found[0]);

    }

}

}

Joey Blake
Any way to make it work with https as well?
Cyclone
Got it working! How can I make it return JUST the domain, totally ignoring the http:// and www?
Cyclone
(http|https)://(www|)(.*?\.(com|org|net))then change the found to reference 3 'found[3]'
Joey Blake
I recommend this tool for testing Regular Expressions http://www.gskinner.com/RegExr/
Joey Blake