View Full Version : Problems Sending Variables from Javascript to an SWF
andynick
07-16-2002, 08:08 PM
Hey all,
I'm having a really tough time. I deal mainly with actionscripting within flash, but I've had to leave my comfort zone and work out some problems through Javascript.
I am aquiring information about the browser, OS, etc. using Javascripting. My code works really well, and I have successfully given value to 2 different variables.
What I need now is a way to get these values back to my flash document. The doc is all ready to recieve them, I just can't get them back. When I attach it to the end of the URL and DEFINE the variable there, it works fine. (example: "http://www.myaddress.com/movie.swf?OpSys=mac")
however, I need to carry the value that I've already given it. when I simply code "http://www.myaddress.com/movie.swf?OpSys" it doesn't recognize the characters OpSys as a variable that I've already assigned a value to.
I am clueless as to what to do. Help is MUCH appreciated!!
andy
HappyDude
07-16-2002, 08:47 PM
When you reference the variable OpSys, you should go outside the quotes... that's a really bad explanation, so here's some example:
document.location="http://www.myaddress.com/movie.swf?OpSys="+OpSys+""
//etc...
//or if you're using a button to move pages
<input type="button" value="Send Info" onclick='document.location="http://www.myaddress.com/movie.swf?OpSys="+OpSys'>
Hope that helps.
andynick
07-16-2002, 09:11 PM
Thanks man, but I'm getting no love from the new code.
Here is exactly what I have.....
<param name=movie value="recieve_var.swf?OpSys="+OpSys+">
<param name=quality value=high>
<embed src="recieve_var.swf?OpSys="+OpSys+" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="300" height="200">
</embed>
I'm exploding with confusion. Seems like it should work to me, but I'm getting nothing. However, I am positive that the variable OpSys contains a value. I just can't seem to pass it....
Any ideas??
thanks!!!!!!!!!!!!!
andy
neil.c
07-16-2002, 09:57 PM
you can't use javascript expressions as parameters inside html tags, except for event handlers eg onclick, onload.
i would suggest putting an id parameter into the <embed> tag, and then later (maybe body onload), assign the src of the embed. like this:
<html>
<head>
<title>untitled</title>
<script language=javascript>
function startMovie(){
//replace this comment with the code to work out the OpSys variable
document.getElementById("theMovie").src = "recieve_var.swf?OpSys=" + OpSys
}
</script>
</head>
</html>
<body onload=startMovie()>
<!--more html here-->
<!-- i don't know what params do so i left them alone. they might not be needed, i don't know about flash.-->
<param name=movie value="recieve_var.swf">
<param name=quality value=high>
<embed id= "theMovie" quality=high
pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"
type="application/x-shockwave-flash" width="300" height="200">
</embed>
<!--more html-->
</body>
any good? :thumbsup:
this is a very basic version and won't work with old browsers.
andynick
07-17-2002, 03:41 PM
Thanks Niel.... I'm working on that code but I still can't get anything to happen.
I think one problem might be this: the way that I am collecting the variables is with a function. Specifically, the code looks like this:
<!--
function checkOS() {
if(navigator.userAgent.indexOf('IRIX') != -1)
{ var OpSys = "Irix";
} else if(
(navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('95') != -1)) {
var OpSys = "Windows"; } else
if(navigator.userAgent.indexOf('Win') != -1){
var OpSys = "WindowsNT or XP"; } else
if(navigator.userAgent.indexOf('Mac') != -1) {
var OpSys = "Mac";
} else {
var OpSys = "other"; } return OpSys;}
// -->
And then..... I use this code directly below that to get my browser information.
var browser=navigator.appName + " " + navigator.appVersion;
var getkey=browser.substring(0, 12);
function loadPage(){
if (browser.substring(0, 8)=="Netscape")
andy="netscape";
if (browser.substring(0, 9)=="Microsoft")
andy="ie";
if ( (browser.substring(0, 8)!="Netscape") && (browser.substring(0, 9)!="Microsoft") )
andy="other";
}
I hate putting that much code into a small message like this, but if it helps anyone understand my problem better, I'm all about it. So, you can see that this is a function. Later in my code, however, in the body of my html, I actually call the function. I believe that is why your code isn't working - I haven't actually run the script until after the body has begun. Here is how I call it:
<body>
<Script>
<!--
var OpSys = checkOS();document.write(OpSys);
// -->
<!-- Activate Cloaking
var browser=navigator.appName + " " + navigator.appVersion;
var getkey=browser.substring(0, 12);
if (browser.substring(0, 8)=="Netscape") {
var andy="netscape";
}
if (browser.substring(0, 9)=="Microsoft") {
var andy="ie";
}
if ( (browser.substring(0, 8)!="Netscape") && (browser.substring(0, 9)!="Microsoft") ) {
var andy="other";
}
// De-activate Cloaking -->
</Script>
The only thing after that code is param lines and the embed lines that load my movie.
Could it be possible that since I'm not calling the function until after my body command, that the onload startmovie() command isn't really sending anything yet?
Please don't be intimidated by all that code!!! It's only there to help you all answer my question. :)
thx again!!
andy
neil.c
07-17-2002, 08:17 PM
you got it in one!
the startMovie() function uses the OpSys variable. if this variable doesn't exist, it can't do anything.
but the setTimeOut should get round thar, cos by the time it runs your OpSys will be defined.
Looks as if there's another problem somewhere. I don't know anything about browser and system identification, I try to just make cross-compatible scripts. So I don't know whether this bit works properly, I can only test it as it is.
I'll have a go at putting it all together and testing.
andynick
07-17-2002, 08:20 PM
thank you Neil!!!!
I appreciate the help more than you know.
Let me know what you come up with. What's setTimeOut? It seems like there should be another way to begin functions other than placing a "onload" in the <BODY> tag. If there was, we could make loading the movie the very last thing that occurred...
I wish I knew more about javascript. Thanks again for your help!!
andy
neil.c
07-17-2002, 08:30 PM
sorry!
ive got more than one thread going just now. setTimeOut is nothing to do with yours, its someone else's thread.
just put startMovie() at the end of the last script that runs. then the OpSys variable will be defined.
neil.c
07-17-2002, 09:01 PM
here is a complete html page.
it should work, i tested it using a <span> because i don't have the flash stuff. i've changed it for the <embed> now.
one thing i noticed, does your .swf movie need quotes around the variable?
the code below sets the embed src to this (on my pc):
receive_var.swf?OpSys=Windows NT or XP
this might need to be
receive_var.swf?OpSys='Windows NT or XP'
i've put a comment in the code about this.
and as a side issue, I'd look over that code again. I run Win 98 SE, not NT or XP.
here's the complete html:
<html>
<head>
<title>untitled</title>
<script language=javascript>
<!--
//i've tidied this code up for clearer reading.
function checkOS() {
if(navigator.userAgent.indexOf('IRIX') != -1) {
var OpSys = "Irix";
}
else if ((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('95') != -1)) {
var OpSys = "Windows";
}
else if(navigator.userAgent.indexOf('Win') != -1){
var OpSys = "WindowsNT or XP";
}
else if(navigator.userAgent.indexOf('Mac') != -1) {
var OpSys = "Mac"
}
else {
var OpSys = "other"
}
return OpSys
}
function loadPage(){
var browser=navigator.appName + " " + navigator.appVersion
var getkey=browser.substring(0, 12)
if (browser.substring(0, 8)=="Netscape")
andy="netscape";
if (browser.substring(0, 9)=="Microsoft")
andy="ie";
if ( (browser.substring(0, 8)!="Netscape") && (browser.substring(0, 9)!="Microsoft") )
andy="other";
}
// -->
</script>
</head>
<body>
<Script language=javascript>
<!-- //executes automatically as page is loaded
var OpSys = checkOS()
var browser=navigator.appName + " " + navigator.appVersion
var getkey=browser.substring(0, 12)
if (browser.substring(0, 8)=="Netscape")
var andy="netscape";
if (browser.substring(0, 9)=="Microsoft")
var andy="ie"
if ( (browser.substring(0, 8)!="Netscape") && (browser.substring(0, 9)!="Microsoft") )
var andy="other"
//-->
</Script>
<embed id= "theMovie" quality=high
pluginspage = "http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"
type="application/x-shockwave-flash" width="300" height="200">
</embed>
<script language=javascript>
<!--
//sets the src of the movie. *cannot* be run until after the embed tag is loaded.
//this is why the script is placed after the embed tag.
document.getElementById("theMovie").src = "recieve_var.swf?OpSys=" + OpSys
//if the movie needs quotes, use this instead of the line above:
//document.getElementById("theMovie").src = "recieve_var.swf?OpSys='" + OpSys + "'"
//-->
</script>
</body>
</html>
does it work for you? :thumbsup:
andynick
07-17-2002, 11:17 PM
You did it.
HORRAY!!!!!!!!
I can hardly believe it!!! You solved the problem! You got it to work. Nice job!!!! Seriously, you deserve a big pat on the back for that one.
Thank's a ton for helping me work through this. Check up on www.nickad.com to see what I do with that beautiful little peice of code.
andy@nickad.com
andynick
07-17-2002, 11:38 PM
Wait a sec....
I'm only getting the movie to load in IE on a PC.
This could be a much harder problem to solve, I would guess.
Do you have any idea how to fix this one?? hahah, I'm really running you ragged here.
Anyways, if there's a quick fix I'll be all over it. Thx again!
andy
andynick
07-17-2002, 11:56 PM
Ok, check me out, I fixed the problem myself. (novel idea huh?)
How did I do it, you ask?
well, since I'm so damn proud of myself I'll tell you. I looked up some technotes and it seems that the embed tag is what really does the trick for IE (on macs) and Netscape (cross platform).
So, I edited the code around the embed that you gave me. Instead of just "embed ID like you had, I added the SRC statement that was my the online path. Then, when that didn't work, I figured that I was missing something, so I placed the embed code within object tags, and whadda you know, it worked.
Thanks again for your help. can't tell you enough.
andy
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.