PDA

View Full Version : Access javascript value from flash


mrbadboy
07-17-2007, 09:44 AM
Hi,

I need to access javascript value from flash or i need to access html controls value(like hidden or text box value) from flash. How can i get i from flash. Can you help me how to get it ? :confused:

- Thanks.

RStankov
07-17-2007, 10:13 AM
I do this by two js-action script codes:



This is the action script code added into the flash

// ----------------------------------------------------------------
//
var JSObserver:Object =
{
call: null, // call property
valid: [], // valid functions
root: _root, // functions root, where to search function for execution
callWatcher: function(prop, oldVal, newVal)
{
// @patern function_name/param1/parem2....
var parts = newVal.split('/');
var fname = parts.shift().toLowerCase();
var _func = null;

// check function name
if (!fname)
{
return newVal;
}

// find valid function (if some)
for(i=0; i < this.valid.length; i++)
{
if (this.valid[i] == fname)
{
_func = this.root[fname];
break;
}
}

// if there was no valid function
if (!_func)
{
return newVal;
}

// call function with parametes
_func.apply(this, parts)

return newVal;
},
setValid: function()
{
this.valid = this.valid.concat(arguments);
},
setRoot: function(func)
{
this.root = func;
}
}
JSObserver.watch("call", JSObserver.callWatcher);


and this is javascript code:

SWFCall = function()
{
// @note: $A is from prototype use other function to convert arguments to array
arguments = $A(arguments);

if (SWFCall._movie)
var movie = SWFCall._movie;
else
var movie = SWFCall.getmovie(arguments.shift());

movie.SetVariable('JSObserver.call', arguments.join('/'));
}
SWFCall.movie = function(movieName)
{
SWFCall._movie = SWFCall.getmovie(movieName);
}
SWFCall.getmovie = function(movieName)
{
if (navigator.appName.indexOf ('Microsoft') !=-1)
return window[movieName]
else
return document[movieName]
}


And I use it in javacript like that


// 1st try
SWFCall.movie('flash_movie_id');
SWFCall('flash_function_name', 'arg1', 'arg2', 'arg3');
SWFCall('flash_function_name', 'arg4', 'arg5', 'arg6');

// 2nd
SWFCall('flash_movie_id', 'flash_function_name', 'arg1', 'arg2', 'arg3');



In flash:

function flash_function_name()
{
// do something
}
JSObserver.setValid('flash_function_name');


notes:
1) The main idea is that JSObserver watch one value if this value is changed(via SWFCall) it check if there was registered a function with function name and call it
2) this is for actionscript v2. It may work with v1 if you delete ':Object', and for actionscript v3 I don't know because v3 is a crap...