Joshc
Joshc

Reputation: 3853

Get the hostname with action script 2

I am creating some campaign swf banners and I don't use action script very often so any help from the experts would be great thanks.

I am supplying my banners on my website as resource downloads. And tutorials of how to embed the swf which has some javascript flashvars.

These flash variable is then concatenated into a google campaign link to change the utm_source.

This is my javascript...

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script type="text/javascript">
    var flashvars = {};
    flashvars.campaignSource = window.location.hostname;
    var params = {};
    params.loop = "true";
    params.quality = "best";
    params.wmode = "opaque";
    params.swliveconnect = "true";
    params.allowscriptaccess = "always";
    var attributes = {};
    swfobject.embedSWF("banner.swf", "banner_mpu", "300", "250", "9.0.0", false, flashvars, params, attributes);
</script>

and my html...

<div id="banner_mpu">
    <a href="http://www.adobe.com/go/getflashplayer">
    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
    </a>
</div>

So the above js works great, however, not everyone will use my tutorial code and will probably use there own methods to embed the swf banner on their site.

So I need some back up action script 2 to get the current hostname into a action script variable

This is my action script which I have so far on my button (swf)...

on(release) {

    function GetTheHostname() {

        var RootFullUrl = _root._url;
        txtFullUrl.text = RootFullUrl;
        var lastSlashIndex:Number = RootFullUrl.lastIndexOf("/");
        var DriveIndex:Number = RootFullUrl.indexOf("|");

        if (DriveIndex>=0) {
        baseUrl = RootFullUrl.substring(0, DriveIndex);
        baseUrl += ":";

    } else {

        baseUrl = "";

    }

        baseUrl += RootFullUrl.substring(DriveIndex+1, lastSlashIndex+1);
        txtBaseUrl.text = baseUrl;
        return baseUrl;

    }

    var campaignSourceAS2:String= GetTheHostname();

    if ( _root.campaignSource == undefined ) {

        getURL("http://www.mysite.co.uk/?utm_source=" + campaignSourceAS2 + "&utm_medium=MPU&utm_campaign=My%20Campaign%202012", "_blank");

    } else {

        getURL("http://www.mysite.co.uk/?utm_source=" + _root.campaignSource + "&utm_medium=MPU&utm_campaign=My%20Campaign%202012", "_blank");

    }

}

The problem with my action script is that it returns the full current URL.

Can any one please help me adapt the GetTheHostname function to get the host name instead of the baseURL


Thanks in advance

Upvotes: 0

Views: 1078

Answers (1)

Will Kru
Will Kru

Reputation: 5222

In that case, I guess it would be as easy as stripping the http:// from the url and then get all that's left to the first /

A one-liner to go from 'http://www.example.com/category/actionscript' to 'www.example.com' would be

var baseURL:String = _root._url.split("http://").join("").split("/")[0];

and to replace your full method

getURL("http://www.mysite.co.uk/?utm_source=" + (_root.campaignSource || _root._url.split("http://").join("").split("/")[0]) + "&utm_medium=MPU&utm_campaign=My%20Campaign%202012", "_blank");

Upvotes: 1

Related Questions