Click to See Complete Forum and Search --> : A simple question?


Sarabellum
04-10-2003, 03:07 PM
I don't know javascript very well, so I thought I'd put a simple question to you all. First, let me explain what I'm trying to do. I'm sending variables from Flash to Javascript, and then I want to send those variables right back to Flash. I followed a tutorial on macromedia's site on how to do this, and the variables are sending from Flash to Javascript just fine, but they don't seem to be sending back to Flash.

Here's the tutorial I used:
http://www.macromedia.com/support/flash/ts/documents/java_script_comm.htm#jtfc

The problem is that I don't want to use a text field to get the variable to send back to Flash, I just want to resend the same one back. Is there a simple way to rewrite this code to work without a text field?

Excerpted for your ease:
In <HEAD>:
<SCRIPT LANGUAGE=JavaScript>
<!--
function doPassVar(args){
var sendText = args.value;
window.document.myFlash.SetVariable("wantSound", sendText);
alert (args);
}
//-->
</SCRIPT>

In <BODY>:
<form name="form1" onSubmit="doPassVar(sendText);" action="#">
<input type="text" name="sendText" maxlength="45" onChange="doPassVar(this);">
</form>

Thanks in advance for your help!

viravan
04-10-2003, 03:14 PM
Which browser are you using (IE5, IE6, NN4, NN6, NN7)???

Basically you are dealing with a Java applet, for the LiveConnect to work, you must enable Java in the browser and you must follow the instruction carefully (i.e., adding the <OBJECT> tag for IE and <EMBED> tag for NN).

:)

V.V.

Jona
04-10-2003, 03:18 PM
Hmmm... V.V., that's odd. <embed> works for IE. It does for me. Take a look at this: http://geocities.com/god_loves_07/dynamicObj.html

Notice that in the script to play the Flash file, I had to use <embed>. <Object> didn't work for me... I'm using IE6.

Sarabellum
04-10-2003, 03:21 PM
IE 6 is my default, and I followed the instructions for the OBJECT and EMBED tags precisely... I even had javascript output the variable with alerts after receiving it from Flash, and that part worked fine.

the question is how do I get rid of the form field?

Jona
04-10-2003, 03:22 PM
Then the question becomes (again?) : do you want this script to work in all browsers?

viravan
04-10-2003, 03:22 PM
Looks to me like you have both <embed> and <object> tags in there, IE just takes the one it likes and ignore the other. Likewise, when you have both tags, NN will take embed and ignore the <OBJECT> which it doesn't know what to do with.

:)

V.V.

Sarabellum
04-10-2003, 03:25 PM
It only needs to work in IE. (It'd be nice if it worked in Netscape, but the agency I'm putting this together for uses IE only, so it's not a requirement.)

Jona
04-10-2003, 03:26 PM
But... I tried using only <object> and it didn't work. But when I added the <embed> inside of it, it worked. Also in this script here (http://geocities.com/god_loves_07/next_mid.html) I used <embed> to play a MIDI, and it works fine for me...

Jona
04-10-2003, 03:31 PM
Wait a second, you're telling us that the code on the bottom of that page that says how to send variables from Javascript to Flash does not work?

viravan
04-10-2003, 03:34 PM
Originally posted by Jona
But... I tried using only <object> and it didn't work. But when I added the <embed> inside of it, it worked. Also in this script here (http://geocities.com/god_loves_07/next_mid.html) I used <embed> to play a MIDI, and it works fine for me...


It's possible, I don't deal with this stuff.....my conclusion is based on the following on the posted link:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/
flash/swflash.cab#version=5,0,0,0"
width=366 height=142 id="myFlash">


Notice swflash.cab -- a cab file is a MS thingy not used by Netscape!

:)

V.V.

Sarabellum
04-10-2003, 03:34 PM
No, I'm saying that it DOES work, but I don't want to do it that way. :-)

viravan
04-10-2003, 03:38 PM
Originally posted by Sarabellum
IE 6 is my default, and I followed the instructions for the OBJECT and EMBED tags precisely... I even had javascript output the variable with alerts after receiving it from Flash, and that part worked fine.

the question is how do I get rid of the form field?


All I can suggest is to play around with the settings under:

Tools->Internet Options-->Security-->Advanced--Java and Microsoft VM


:)

V.V.

Jona
04-10-2003, 03:39 PM
Um... Try fscommand("send_var", inputVar); and in your Javascript: var inputVar = sendText; (or the original source of it, and create a whole new function).

viravan
04-10-2003, 03:43 PM
but they don't seem to be sending back to Flash.


Do you see a little yellow icon on the status bar (at the bottom of the browser screen). If you do, I am 99% certain that it says something to the effect that window.document.myFlash is null or not defined.

Tell me what you see.


:)

V.V.

Sarabellum
04-10-2003, 03:48 PM
Perhaps I should re-explain myself. The code example I posted works just fine. No problems with Java. The problem is the tutorial I used doesn't do exactly what I want it to do.

Let me explain why I'm doing what I'm doing. I'm loading in a series of .swf files, and I need to hold a variable so that Flash knows whether to play sound or not. I just want to be able to send this variable from Flash to Javascript each time a user turns the sound on or off. This is working perfectly.

Then what I want to do is... after I load the next .swf file, use javascript to send that variable back to Flash, so that the new Flash file knows whether the sound was turned off or on in the previous Flash file. Does this make sense?

The problem being that I don't want the user to have to type in true or false in a text field on the html page to tell Flash to turn sound on or off... this should be completely invisible to the user.

So, I followed this part:
http://www.macromedia.com/support/flash/ts/documents/java_script_comm.htm#ftjc

Now I just want to take that and send it back to Flash, but not with a text field, which is the only way explained in the tutorial posted previously.

Jona
04-10-2003, 03:49 PM
I just tested the script. I get no errors, and I get alerted, "[object]" I used both window.document.myFlash and myFlash (without window.document on there). Both work the same.

Sarabellum
04-10-2003, 03:50 PM
No, there is no yellow icon, because there are no javascript errors being returned. It's working fine... I'm asking for a rewrite. :-)

Jona
04-10-2003, 03:55 PM
Well in that case... :p Instead of using a form field, use Flash to send the variable to a Javascript variable. Just take the whole form field off like this:

<SCRIPT LANGUAGE=JavaScript>
<!--
function doPassVar(args){
document.myFlash.SetVariable("wantSound", args);
alert(args);
}
//-->
</SCRIPT>

The only thing now is, you have to send the Flash variable to Javascript.

Sarabellum
04-10-2003, 04:05 PM
Thanks Jona... that's what I was getting at!

Now, the minor problem... I tested that, and it works great... but I have a new question now...

The code you just posted sends the variable back and works properly, but it sends it back immediately, which means it gets sent to the first .swf file.

Is there a way to force this function to run again as soon as I load in my next Flash file? (In other words, do you know if I can call the doPassVar function from Flash?) (I'm hunting around for tutorials as I type...)

Jona
04-10-2003, 04:26 PM
You can use setTimeout(), or you can use Flash to call a Javascript function onStart. So on your second .swf file, put some ActionScript that starts immediately, and uses getURL. Then get the URL of javascript:doPassVar();

Sarabellum
04-10-2003, 04:34 PM
Okay, I tried this:
get the URL of javascript:doPassVar();

getURL("javascript:doPassVar()");

but then my alert returns "undefined"...

and I can't do the TimeOut method, because the user decides when to continue to the next movie. Hmmm... thinking thinking...

I tried to feed it args and that got me nowhere... also tried to change it to javascript:doPassVar; and it didn't like that one. :-) hehe

Anyway to call it without passing it a value?

Sarabellum
04-10-2003, 04:37 PM
And there's not actually a space there between the java and the script. :-)

Sarabellum
04-10-2003, 04:43 PM
By the way... Thanks for your speedy responses! I really really really appreciate your help! :)

Jona
04-10-2003, 04:52 PM
The original method should work fine. Just add a hidden link to your page: <a href="javascript:doPassVar();" stlye="visibility:hidden;"></a>

Then try using getURL.

Sarabellum
04-10-2003, 05:16 PM
Okay... I tried that, and corrected the typo on "style" but it still doesn't work... hmm... is there a way to modify the original working text field to make the variable from Flash come into that, and make the text field hidden?

This is what the code looks like to process the variable coming in from Flash:
<SCRIPT LANGUAGE=JavaScript>
<!--
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
function myFlash_DoFSCommand(command, args) {
var myFlashObj = InternetExplorer ? myFlash : document.myFlash;
alert (args);
}
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
document.write('<SCRIPT LANGUAGE=VBScript\> \n');
document.write('on error resume next \n');
document.write('Sub myFlash_FSCommand(ByVal command, ByVal args)\n');
document.write(' call myFlash_DoFSCommand(command, args)\n');
document.write('end sub\n');
document.write('</SCRIPT\> \n');
}
//-->
</SCRIPT>

Is there a way I can then write the "args" coming down there into the original text field method?

Jona
04-10-2003, 05:21 PM
How about we go back to the original one with the text field, but use this: <input type=text name="sendText" style="visibility:hidden;position:absolute;top:1;left:1;">