Jay Wit
Jay Wit

Reputation: 3057

How to get URL Param's to Flash?


I'm working on a Flash website for a while now, but just now I noticed that the link doesn't change (that's normal for a Flash application). But the thing is, the website needs to be 'shareable'. So my problem is;
How can I get a param from the URL and read it in Flash?
For example, when the url is http://www.thewebsite.ext/index.php?article=245
The Flash application has to know that the article is 245 so it can go to the right article instead of just the homepage.
How can this be done?
(By the way, I'm using actionscript 2)

Edit:
Here is the source code of the SWF file;

<script type="text/javascript">
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','100%','height','100%','id','PortfolioScroller','src','media/casescroller2','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','wmode','opaque','movie','media/casescroller2', 'FlashVars', 'pfcase=1' ); //end AC code
</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="100%" height="100%">
    <param name="movie" value="media/casescroller2.swf?pfcase=1" />
    <param name="quality" value="high" />
    <param name="id" value="PortfolioScroller" />
    <param name="wmode" value="opaque" />
    <param name="FlashVars" value="pfcase=1">
    <embed src="media/casescroller2.swf?pfcase=1" FlashVars="pfcase=1" width="100%" height="100%" id="PortfolioScroller" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" wmode="opaque" name="PortfolioScroller"></embed>
  </object>
</noscript>

ActionScript 2;

_root.movieclip1.visible = false;
var pfcase;
if(pfcase){
   _root.movieclip1.visible = true;
}

This script is just to test things out. (a MovieClip becomes visible if pfcase is not empty?)

Upvotes: 1

Views: 2780

Answers (4)

Lars Bl&#229;sj&#246;
Lars Bl&#229;sj&#246;

Reputation: 6127

You can get the URL of the page (or just the parameters) by calling a JavaScript through ExternalInterface.

This is a piece of AS3 code I recently sent to a colleague (somewhat modified here), I believe it will work in AS2 too:

// Get the entire URL for the page, like "http://www.thewebsite.ext/index.php?article=245"
var url:String = String(ExternalInterface.call("function() { return String(window.location); }")).toLowerCase();

// Split the string on the argument that we are after, incl. the =
var parts:Array = url.split("article=");

// parts[1] will be the string following "article=", or undefined if there was no "article=" in the URL
if(parts[1])
{
    // There was an article argument. 
    // parseInt() can be used to convert it to an int, and will also get rid of whatever comes after, if anything.
    // For example, parseInt("123&foo=bar") will return 123, ignoring what comes after the last number in the string.
    trace(parseInt(parts[1]));
}

You could use document.location.search instead of document.location, to get only the argument part of the URL, including the ?, like "?article=245" in this case.

Edit: But since you tagged the question with PHP, I agree with Tania and shadyyx that suppling the value to the Flash application using flashvars is probably better, since it doesn't rely on JavaScript and support for ExternalInterface, and also will make the values immediately available as soon as the swf is loaded. I'll leave the JavaScript/ExternalInterface solution here anyway, it may come in handy for someone.

Upvotes: 1

shadyyx
shadyyx

Reputation: 16055

By using flashvars. In Flash You have to listen for concrete flashvar, e.g. articleUrl and in HTML there must be set

<object ... >
     <param name="FlashVars" value="articleUrl=http://www.thewebsite.ext/index.php?article=245"/>
</object>

OR

<embed ... FlashVars="articleUrl=http://www.thewebsite.ext/index.php?article=245" ... />

Upvotes: 0

goliatone
goliatone

Reputation: 2174

Another approach would be to use SWFAddress. This uses the url hash, so your url would be like so:

http://www.thewebsite.ext/#article=245

There are examples included in the download.

Upvotes: 2

Tania Petsouka
Tania Petsouka

Reputation: 1424

You should flashvars to make this work. http://kb2.adobe.com/cps/164/tn_16417.html

Upvotes: 0

Related Questions