Forum Moderators: open
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]
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?
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!
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!
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).
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.