Erik Liddell is an interactive designer based out of Boston MA. He graduated from The School of The Museum of Fine Arts Boston where he focused in Experimental Film and Multi-Media Design. The "getUrl();" function in flash is one of the simplest and most widely used scripts in Flash and many of you will probably say, "Man, I am a 'getUrl();' guru!"
I know thats what I would have said until I switched my default browser from IE to Firefox and started noticing a mind boggling bug. I was building a Flash AP to run off a CD-ROM and using get url to load pdfs from that CD. To my suprise my simple and direct getUrl(); script was worknig fine in Internet explorer, but for some crazy reason, in Firefox it was loading an empty page or even numerous empty tabs. (this bug is on a windows based system) This bug has to do with the way firefox interprets a filr - url, how Windows calls your default browser, and how flash issues that command line to Windows.
This tutorial will explain the bug, and show the solution in both AS 2.0 and 3.0
It will go over strings, substrings, indexOf
When your computer calls a file like a PDF to your default browser it does so with a command that looks something like this:
yourBrowser -url "file:///c:your.pdf
When using getUrl(); to say:
getUrl("your.pdf","_blank", "GET");
flash actually posts a command like this:
yourBrowser -url "file:///c|your.pdf
Note the Red. Flash changes the ":" to a "|"
Now for what ever reason, IE automaticly interperets them both as a semi colon or ":" But Firefox seems to interperet the "|" as a new tab.
So what do we do? Well we are going to create our own URL, convert it to a string and replace the "|" with a ":"
The first step of this is to find out the directory our SWF resides in and turn that into a variable.
[as]var swfDir:String = _root._url;[/as]
next we find the last slash in that string:
[as]var lastSlash:Number = swfDir.lastIndexOf("/");[/as]
then we find the pipe or "|":
[as]var colonSpot:Number = swfDir.indexOf("|");[/as]
Now we are going to create and empty string and piece all that stuff together replacing the "|" with a ":".
[as]var myUrl:String;
if(colonSpot>=0){
/*that means if we found a "|" if we didn't the colonSpot value would be -1*/
myUrl = swfDir.substring(0,colonSpot);
/*that makes our Url var = everything BEFORE the "|"*/
myUrl +=":"
/* that ads the ":" */
}else{
myUrl="";
}
myUrl+= swfDir.substring(conolSpot+1, lastSlash+1);
/* now we just added back everything after the | */[/as]
ok Now we write our getUrl() with our new variable "myUrl"
AS 2.0
[as]myButton.onRelease = function()
{
var targetUrl:String = myUrl + "yourfile.pdf";
getURL(targetUrl, "_blank");
};[/as]
AS 3.0
[as]function newGetUrl(event:MouseEvent):void{
var targetUrl:URLRequest = new URLRequest(myUrl + "myfile.pdf");
navigateToURL(targetUrl, "_blank");
}
myButton.addEventListener(MouseEvent.CLICK, newGetUrl);[/as]
That’s it, another bug work around.