4

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!

Cyclone
  • 17,939
  • 45
  • 124
  • 193

3 Answers3

5

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
  • 3,451
  • 2
  • 18
  • 16
  • Got it working! How can I make it return JUST the domain, totally ignoring the http:// and www? – Cyclone Jan 18 '10 at 19:02
  • (http|https)://(www|)(.*?\.(com|org|net)) then change the found to reference 3 'found[3]' – Joey Blake Jan 19 '10 at 00:31
  • I recommend this tool for testing Regular Expressions http://www.gskinner.com/RegExr/ – Joey Blake Jan 19 '10 at 00:32
  • 2
    dont use RegEx, simply check for the third "/" character - that will mark the end of the domain name in your string. The above RegEx will fail for any other domain than .com/.net/org, plus for some other cases too, like: http://cars.com.nu/ – sydd Mar 10 '12 at 21:22
  • keep in mind that if your swf is being loaded through an iframe (e.g. you embed a game on a portal), then `loaderInfo.url` will return the url of the swf in relation to the *iframe*, not the page holding it, thus the domain can be wrong – divillysausages Mar 12 '14 at 13:42
2

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.

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
2
var domain = "http://www.example.com/";
var pathArray = domain.split("//");
pathArray = pathArray[1].split("/");
trace(pathArray[0]); //traces www.example.com
yodalr
  • 9,778
  • 10
  • 32
  • 47