PDA

View Full Version : Variables to Flash From Browser


CookieDog
03-30-2005, 10:30 PM
I did a search, but could not quite find the right answer to my question. I need to have a variable from my HTML page sent to my Flash movie that is embedded in the same HTML page.

I tried doing this with parameters added to the URL, like this:

www.somewhere.com?param1=1&param2=2

However, I couldn't figure out how to get the parameters into the Flash swf. I tried this code, but I couldn't get it to work:

Javascript code on HTML page:
<script language="JavaScript" type="text/JavaScript">
myHref = document.location.href;
myHrefParams = myHref.substr(myHref.indexOf('?'));

myContent = "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'";

myContent += "codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0'";

myContent += "WIDTH='100%' HEIGHT='100%' id='main' ALIGN=''>";

myContent += "<PARAM NAME=movie VALUE='main.swf'" + myHrefParams + "'><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#FFFFFF>";

myContent += "<EMBED src='main.swf' quality=high bgcolor=#FFFFFF WIDTH='100%' HEIGHT='100%' NAME='main'";

myContent += "ALIGN='' TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/go/getflashplayer'></EMBED>";

myContent += "</OBJECT>";

document.write(myContent);

</script>

Then I tried to read the parameters sent with the URL in the Flash swf, but they never show up. All I get from the code below is the URL to the actual swf file (e.g., www.somewhere.com/main.swf). I really want the URL to the HTML page - the location.href - is there a way to read that in Flash?
s = unescape (_URL);
ueURL = s.substr(s.indexOf ("?")+1,s.length);
myTestLine.text = ueURL;//Just for testing to see what I get

Any help is appreciated. Thanks!

[Sx]
03-30-2005, 10:51 PM
Didn't read your whole post, but your end is done wrong - Flash automatically parses parameters passed to it as GET-type query, and builds those variables on _level0

So, if you've printed the query nicely after the .swf name, and with example of http://www.somewhere.com/?param1=1&param2=2 , in Flash you would be able to access those variables as:

myTestLine.text = "param1: " + _level0.param1 + "\nparam2:" + _level0.param2;

Hope it helps...

CookieDog
03-31-2005, 03:31 AM
This didn't work. Wouldn't I have to do a LoadVars or something to get the variables as you describe?

Maybe you should read my entire post, and let me know if you see anything else.

Thanks!

CyanBlue
03-31-2005, 03:35 AM
You are looking for 'FlashVars'... :)

http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_16417

[Sx]
03-31-2005, 10:56 AM
Yup, you can use 'FlashVars' as CyanBlue suggested, but they are supported only with Flash 6+ plugins, so if you aim 'old-plugin' market, or you want to cache the movie based on parameters passed to it, or you just want it the way you've tried to make, in HTML make something like:

<script language="JavaScript">
movieSize = [640,480];
movieURL = document.location.toString();
movieURL = movieURL.split("?");
movieURL = 'movie_name.swf?' + movieURL[1];

document.write('<object codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + movieSize[0] + '" height="' + movieSize[1] + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">');
document.write('<param name="movie" value="' + movieURL + '">');
document.write('<param name="menu" value="true">');
document.write('<param name="quality" value="high">');
document.write('<param name="scale" value="exactfit">');
document.write('<embed src="' + movieURL + '" menu="true" scale="exactfit" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="' + movieSize[0] + '" height="' + movieSize[1] + '">');
document.write('</embed></object>');
</script>

Of course, make sure that movie_name.swf is replaced by name of your .swf file, and you have your movie's width and height in movieSize array...

This should (didn't test it, but that's the way to do it) print your specified flash movie passing to it all the variables on traditional way - GET-type query after the movie name...

After that, you should be able to access all the passed variables from _level0 ... e.g. if you've put this code in 'home.html' file, which loads some of your movies, if you call it with 'path/to/home.html?foo=bar', from your Flash move you should be able to access _level0.foo variable which would have 'bar' as value...

Hope it helps...

CyanBlue
03-31-2005, 02:35 PM
Yup... That's got to work... But I hate that form... Don't know why, don't ask... ;)

Just a quick question, Sx...

You have 'version=6,0,0,0' in the code... I don't know if that happened because you did copy and paste or not, but that's not importane...

If you have that string, it will look for the Flash Player on the user's computer to see if the version is greater or equal to that given version number and only display the Flash object when the condition is met and forward to Macromedia's page if not...

Can you see if what I have above is correct or not??? :)

[Sx]
03-31-2005, 04:13 PM
I've copied <object>/<embed> structure from the first HTML I've grabbed... I'm not crazy to type all that... Time is money ;)

Anyways, that cab version doesn't matter much as I know in players older than 7,00,14 (or was some of the earlier versions able to auto-update), and certainly not to non-IE users (read it: normal, sane, non-masochistic people :D )... So that param in RL is ignored (again, I'm not 100% sure about that - I wouldn't touch IE with a stick... not even in a radiation-proof suit... from 5 miles away)... But you feel free to try it and tell us what happens... Again, i think that even if you put 9,0,0,0 nothing would happen...

But if it's causing unneeded updates, well, it's easy to put it to 4,0,0,0 and 'no problemo' ;)

CyanBlue
03-31-2005, 06:31 PM
Well... I tested with F5 player with the site that says 6.0.79.0 and it did not forward me to the Macromedia site... (I guess that site needs FPI or something...)
Also did a test with F6 player with the site that says 7.0.0.0 and it just played the movie... Wicked... :D

[Sx]
03-31-2005, 06:43 PM
Told you, cab version has nothing to do with it... Maybe only with 7.xx players that have auto-update ability, but I'm not sure about that one either...

Epilogue: Don't doubt in Sx' judgement :p

CookieDog
03-31-2005, 08:24 PM
Okay, I got it to work. My problem was pretty simple - I had an extra quote or two where I was trying to concatenate my variable as part of the movie swf name (doh!).

Anyway, my original way of doing it worked, as well as just reading the variables from _level0. (Thx)

But, now how would I get this information to be updated from the URL (location.href - NOT the path to my swf movie), without actually going to a new HTML page or swf? Is there a way to do that?

For example, when I use anchors for the Back Button in Flash, the URL actually changes like this:
www.abc.com/index.html#anchor1
www.abc.com/index.html#anchor2, etc.

Would I be able to read that URL, and send the variable of the anchor name to my swf movie as it changes (not just the first one, which is what it does now, without updating)?

CookieDog
04-05-2005, 04:25 AM
But, now how would I get this information to be updated from the URL (location.href - NOT the path to my swf movie), without actually going to a new HTML page or swf? Is there a way to do that?

For example, when I use anchors for the Back Button in Flash, the URL actually changes like this:
www.abc.com/index.html#anchor1
www.abc.com/index.html#anchor2, etc.

Would I be able to read that URL, and send the variable of the anchor name to my swf movie as it changes (not just the first one, which is what it does now, without updating)?

Is this possible? Thanks!

CookieDog
04-06-2005, 06:41 PM
I think this will work. Can anyone tell me if there is a problem with different browsers, or anything else?

The problem with adding the query string on the end of the flash movie is that the html page is not refreshed, so my value is always the same as it was when the page first loaded. In order to have the variable update based on actions in the swf file, I called a javascript function from within the SWF:
getURL("javascript:getURL_Location();");
//Javascript function on HTML page
//This code is added AFTER the Flash OBJECT/EMBED since it references that object - the object has to exist first
//'myText' is the variable name of a text field in the swf for testing purposes
//'swfIDHere' is the swf ID in the OBJECT/EMBED tags
function getURL_Location() {
window.document.swfIDHere.SetVariable("myText", location.href);
}

This generally works, but is a little buggy. I think a delay needs to be added, so that the URL has time to change before the function is actually called because sometimes it doesn't change to the correct URL value, but it usually does. Do you think there is some other problem with this? Or any suggestions to improve? I want to make sure it works in all browsers! Thanks!