Getting the domain name of the page displaying the swf

Posted: August 5th, 2009

I was recently involved with a project that required tracking links from a Flash widget marketing campaign. The problem is, that Flash player does not send the referer in the header or allow the referer to be set programatically. So the solution is to set up a campaign that includes the referer as a GET string in the url. So, how do you get the domain name of the page that the Flash is embedded on? Well a good way is to use the ExternalInterface class. Like this:


referer = ExternalInterface.call('eval', 'window.location.hostname');

Then you just concat the referer var onto the end of your URLrequests. Simple right? Not quite. See, the client was encouraging their associates to put the widget onto social networking sites and personal blogs. Well apparently many of these sites strip out the allowScriptAccess=’always’ param of the embed/object tags. So Flash Player would pop up a screen that says:

ActionScript Security Error 2060: Security sandbox violation: ExternalInterface caller http://www.whateverdomain.com/whatever.swf
cannot access
http://www.whateverotherdomain.com/

So, what’s the fix?

I was able to work around this by using a try/catch block. I caught the security error and ran a couple string.split()’s:


var referer:String;

try
{
	referer = ExternalInterface.call('eval', 'window.location.hostname');
}
// We can get the domain name out of the Security Violation error message
catch (e:SecurityError)
{
	trace(e);
	// just run some string.split's and we got it
	var splitter:Array = emessage.split('//');
	if (splitter.length > 2)
	{
		var splitter2:Array = splitter[2].split('/');
		referer = splitter2[0];
	}
}

Leave a Reply