Code for deeplinking from and with Flash

Posted on April 20, 2009

2


Deeplinking with Flash is a hack. SWFAddress uses embedded JavaScript in the HTML and ExternalInterface to do it. This example tackles the basics of deeplinking and shows you how to do it from Flash alone. (See sample here.)

Just copy&paste the code below in Flash, compile the movie and run it from a web location.

import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldType

import flash.external.ExternalInterface;

/*
    DEEPLINKING
    You can set and get the URI of flashcontent using Flash alone.
    This script shows you how.

*/

// CREATE TEXT FIELDS
createTextItem(30,500,TextFieldType.DYNAMIC,"PAGE URL:")
var url:TextField=createTextItem(50,500,TextFieldType.DYNAMIC)
createTextItem(80,500,TextFieldType.DYNAMIC,"PAGE URI:")
var uri:TextField=createTextItem(100,500,TextFieldType.DYNAMIC)
createTextItem(130,500,TextFieldType.DYNAMIC,"TYPE YOUR URI HERE AND WATCH THE ADDRESS BAR:")
var uriin:TextField=createTextItem(150,500,TextFieldType.INPUT)
uriin.addEventListener(Event.CHANGE,setUri)

// SET TIMER
var changeListenTimer:Timer = new Timer(2000);
changeListenTimer.addEventListener(TimerEvent.TIMER, getUrl);
changeListenTimer.start();

// GET URL NOW
getUrl(null);

//=========================================
// START OF CODE TO READ AND SET URI
//=========================================

var myUrl:String;
var myUri:String;
var myUriStart:String="#/";

function getUrl(evt:*)
{
// GET PAGE URL
    myUrl=ExternalInterface.call('window.location.href.toString');
    myUri="";
// PRESENT IN TEXTFIELD
    if(myUrl.indexOf("#")>0)
    {
       // GET URI
        myUri=myUrl.substr(myUrl.indexOf("#")).split(myUriStart).join("");
        // GET BASE URL
        myUrl=myUrl.substr(0,myUrl.indexOf("#"))

    }
    this.url.text=myUrl;
    this.uri.text=myUri;

}

//=========================================
// SET URI
//=========================================
function setUri(evt:*)
{
    // You can add to "#/" to anything like: "#myThingy/".
    // replacing myThingy for your preference.
    //Apparenly AppleMail cuts hyperlinks using "#/"

    var url:String =this.url.text + myUriStart + this.uriin.text;
    var request:URLRequest = new URLRequest(url);
    try {
      navigateToURL(request, '_self');
    } catch (e:Error) {
      trace("Error occurred!"); // Laziness
    }
}

//================================
// CREATE TEXT FIELD
function createTextItem(_y,_width,_type,myText:String=""):TextField
{

    var txt:TextField = new TextField();

    var tf:TextFormat;
    tf = txt.getTextFormat();
    tf.color = 0x000000;
    tf.size = 14;
    txt.defaultTextFormat = tf;
    txt.width=_width;
    txt.height=20;
    txt.y=_y
    txt.x=20
    txt.setTextFormat( tf );

    if(_type==TextFieldType.INPUT)
    {
    txt.border=true;
    txt.borderColor=0x000000;
    }
    else
    {
        txt.mouseEnabled = false;
        txt.selectable = false;
    }

    txt.type=_type;

    txt.text = myText;

    this.addChild(txt);

    return txt;
}
Advertisements