Forum Moderators: open

Message Too Old, No Replies

Help passing vars to flash

pass variables to flash from the head section of html

         

Fiasst

5:14 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



Hello,

I trying to send variables to a Flash movie from my html page. I found this example which sends/receives data when you click a button:

[permadi.com...]

The problem is, I need the variables sent without the user doing anything – as soon as the page loads. So I’ve been trying onLoad in JavaScript. This works in IE but not in FireFox/Opera.

Here is my example:
<url removed>

Can you see a reason for this?

Also is there a better way to pass variables to flash from the head section of my xhtml pages?

Thanks!

[edited by: encyclo at 8:37 pm (utc) on April 6, 2007]
[edit reason] no URLs please, see TOS [webmasterworld.com] [/edit]

Fotiman

5:52 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, personal URLs are not permitted.

That said, the head of your code example looks like this:


<script type="text/javascript" src="checkmate.js"></script>
<script type="text/javascript">
varr1 = "this is var 1";
varr2 = "this is var 2";
</script>

And your checkmate.js method tries to access the varr1 and varr2 variables when it tries to set the value in your flash movie. Have you tried moving the checkmate.js script to load AFTER you set those values?

Fotiman

5:57 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Alternatively, could it be that your Flash object just isn't ready for the values yet? That is, maybe the Flash object hasn't finished loading yet? I added breakpoints and could see that you are sending valid data, which suggests to me that the Flash object just isn't ready to process it yet.

Fiasst

4:29 pm on Apr 8, 2007 (gmt 0)

10+ Year Member



Thanks for your replies.

Ah ok, I didn't know personal URLs are not permitted.

I changed the order of the scripts. checkmate.js is now under the two vars like so:


<script type="text/javascript">
varr1 = "this is var 1";
varr2 = "this is var 2";
</script>
<script type="text/javascript" src="checkmate.js"></script>

I think my flash movie is ready for the vars because IE has no trouble accepting them. Its Firefox and Opera that are struggling which is a bit of a coincidence.

When I refresh my page on my local machine using Firefox I get a Flash security alert box warning me of the two files trying to communicate. Maybe Safari browsers block certain functions from running ‘onLoad’?

Thanks for your help!

Fiasst

5:46 pm on Apr 8, 2007 (gmt 0)

10+ Year Member



Seeing as the Link I provided to my example has been edited out below is the code I'm using.

XHTML page:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>flash test</title>
<script type="text/javascript" src="checkmate.js"></script>
<script type="text/javascript">
varr1 = "this is var 1";
varr2 = "this is var 2";
</script>
</head>
<body>
<script type="text/javascript">test1();</script>
<input type="button" value="Set" name="set" onClick="setVars();" />
</body>
</html>

external JavaScript:


window.onload = function () {
setVars();
}
function test1() {
document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="200" height="200" id="test1" />\n');
document.write('<param name="movie" value="test1.swf" />\n');
document.write('<param name="quality" value="high" />\n');
document.write('<param name="swliveconnect" value="true" />\n');
document.write('<embed src="test1.swf" width="200" height="200" name="test1" swliveconnect="true" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"/>\n');
document.write('</embed>\n');
document.write('</object>\n');
}
function getMovie(movieName) {
if (window.document[movieName]) {
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1) {
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return document.getElementById(movieName);
}
}

function setVars() {
var flashMovie=getMovie("test1");
flashMovie.SetVariable("/:var1", varr1);
flashMovie.SetVariable("/:var2", varr2);
}

I can send a link to the flash movie via Private Message if needed.

Thanks!

Fotiman

2:56 pm on Apr 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just for an experiment, try changing your onload method to this:


window.onload = function () {
setTimeout(setVars,2000);
}

My suspicion is that the flash object is just not ready to process the values when you're passing them in. IE may be loading the flash object faster. What the example above will do is wait 2 seconds after the onload event before calling the setVars method. If it works, then I think it's just a timing issue (your movie is not finished loading in time).

Fiasst

5:49 pm on Apr 9, 2007 (gmt 0)

10+ Year Member



wow that worked!

That's awesome thank you :D

Is it a bad idea to use really short timeouts? like this:


window.onload = function () {
setTimeout(setVars, 00001);
}

I do need the vars to be passed as soon as possible so the flash movie can start loading the correct images.

Thanks again!

Fotiman

7:18 pm on Apr 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, so I was right then about the Flash object simply not being ready. Which means what you really want to do is have the flash object notify your script when it's ready, or have it simply pull in the values when it's ready. The problem with trying to use setTimout and some small value is that it's probably not a constant time. That is, it's probably dependant on the client, so you might have one guy viewing with a really fast computer and one with a really slow computer, and when the Flash finished loading might be different for both of them.

I'm not a Flash developer, but I'm pretty sure that you can add onLoad handlers to your flash code. You might try posing this question in the Flash forum.