Given that there is a global javascript variable on my web page named myVar, how can I access the value of the variable myVar from within my flash movie using javascript?
I see plenty of examples of using external interface in order to execute javascript from actionscript, but I am unable to find examples of returning values back into the flash movie using actionscript.
Thanks in advance. I hope my question is clear enough.
ExternalInterface works by allowing JavaScript to invoke an ActionScript function in the movie, and vice-versa. You can optionally receive a return value back from the invoked function. Here's a very simple example:
JavaScript:
<script language="JavaScript">
function getMyVar()
{
return myVar;
}
</script>
Flash/AS:
import flash.external.ExternalInterface;
var result:string = ExternalInterface.call("getMyVar");
You can also provide an anonymous function that returns your global variable value to the ExternalInterface.call method as follow:
ExternalInterface.call("function(){ return myGlobalVariable; }");
I've noticed Rex M's answer is a bit incomplete.
He was right about using...
import flash.external.ExternalInterface;
var result:string = ExternalInterface.call("getMyVar");
Then in your javascript you can use
<script language="JavaScript">
function getMyVar() {
return myVar;
}
</script>
However in order to use this, the flash movie must be in an html accessed over http. Not using file://
Here is a tutorial for communicating from actionscript to javascript and vice versa.
http://www.youtube.com/watch?v=_1a6CPPG-Og&feature=plcp
You can also do this:
ExternalInterface.call("eval","getVar=function(obj){return obj}");
var yourVar:String = ExternalInterface.call("eval","getVar(JSvar)");
Related
Is there a way to do this?
<'param name = "ticket" value = "getTicket()">
getTicket() is my Javascript function. Whatever the function returns, that should be the value of the parameter tag.
I cant set it explicitly using Javascript "document.getElem...." because I want the value to be loaded at the time of page load. Or, while the parameter is being set.
For further info, I am trying to do this for Tableau Trusted Authentication.
Are you using server-side scripting?
Isn't more effectively to do what you do using the request/response ways?
Like directly using <%=ticketValue%> (ASP way) or ${ticketValue} (JSP way)?
You may use Document.Write as follows:
<'param name = "ticket" value = "<script>
document.write(getTicket());
</script>">
You can access node from JavaScript right after it was created.
You're not obligated to wait for DOMContentLoaded event. So this code will work as expected:
<script>
function getTicket() {
return 'whatever';
}
</script>
<param name="ticket" value="" class="js-ticket">
<script>
var ticket = document.querySelector('.js-ticket');
ticket.value = getTicket();
console.log(ticket);
</script>
See demo.
This approach is better than using document.write, see good explanation.
You can use jquery $(document).ready() function callback. If jquery isn't available, then you can use the equivalence of it.
$(document).ready equivalent without jQuery
Then you can access document.getElement.
I've got an AS3 code that I want to assign in JavaScript as a variable like so:
var Name = ;
If I want to set a simple stylesheet I would use:
var Name = { Name.width:500 }
etc.
How do I do that in AS3?
i want to do it like so
var As3 = action script code;
i want to give a var to the actionscript so i can use it later
A SWF on a webpage can interoperate (call functions, set variables, etc) with JavaScript using ActionScript's ExternalInterface.
For example:
ExternalInterface.call("console.log", "Hello JavaScript world!");
Apparently calling eval only works in some browsers, but you can proxy the eval as noted in this answer.
ExternalInterface.call("eval", "window.foo = 'bar'");
Of course, your CSS example is very malformed... You might could:
ExternalInterface.call("eval", "document.getElementById('myDiv').style.width='50px'");
Google for more examples of using ExternalInterface to set variables, call functions, setup callbakcs, etc.
I'm using a webbrowser control in visual studio 2010 to invoke JS script.I'm able to call a function from the web browser but i'm looking to get a varible value from JS and use it in the winform.
I have this JS code for exemple :
<script type="text/javascript">
function f() {
var val=0;
return val;
}
</script>
The C# code is not working :
webBrowser.Document.InvokeScript("f");
what's the right way to invoke the JS function and get the variable value?
InvokeScript will return the value which the javascript function returns. You just need to be a bit careful about it's type. Numbers and strings will be returned a c# strings.
As you can tell by the title, I am having some trouble with AS3 ExternalInterface and jQuery / swfobject.
ActionScript :
if (ExternalInterface.available) {
ExternalInterface.call('init');
ExternalInterface.addCallback('testFunc', returnFunc)
}
function returnFunc():void {
ExternalInterface.call('alertFunc');
}
jQuery:
function init() {
alert('init');
$('#swf_object').testFunc();
}
function alertFunc() {
alert('finished');
}
Obviously that implies the object has the id 'swf_object'
I have also tried getting the object by the following:
document.getElementById('swf_object')
document.getElementById('swf_object')[0]
$('#swf_object')[0]
To no avail.
It's giving the first alert ('init') but then not doing the last one. I'm completely baffled and hope someone can point out my mistakes! (there's bound to be a massively obvious one somewhere)
The problem is that you're calling out to the JavaScript init() which calls the Flash testFunc() before you're made testFunc available (which happens only after the call out to init() completes).
To fix this, simply swap the two lines to this:
ExternalInterface.addCallback('testFunc', returnFunc); // Needs to be available before it's used
ExternalInterface.call('init');
As for getting the Flash object in JavaScript, you can do it directly with document.getElementById('swf_object'), but it's possible using jQuery too:
var swf = $('#swf_object').get(0); // Get the actual object without the jQuery wrapper
i apologize if my terminology is off, my actionscript skills are pretty weak sauce.
so, i have some actionscript that makes a
ExternalInterface.call('someFunction');
call.
is it possible to reference the html object that made the call to someFunction directly using the ExternalInterface.call call?
Assume that the object that makes the call also has some Callbacks (via ExternalInterface.addCallback) that are accessible via javascript.
Currently:
Actionscript source
ExternalInterface.call("someFunction");
ExternalInterface.addCallback("someCallback",someASfunction);
Javascript source
function someFunction(){
document.getElementById('idOfSWFObject').someCallback();
}
I'm thinking there must be a way of:
Actionscript source
ExternalInterface.call("someFunction",THE_OBJECT_MAKING_THE_CALL);
ExternalInterface.addCallback("someCallback",someASfunction);
Javascript source
function someFunction(o){
o.someCallback();
}
once again, sorry about the terminology. tried to lace it with as many keywords for future searches.
thanks!
I guess you are talking about ExternalInterface.objectID. This property returns an id associated with flash container in object or embed tag.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html?filter_flex=4.1&filter_flashplayer=10.2&filter_air=2.en#objectID
I suggest that you should also pass the name of "someCallback" to you JS method. This way there will be no need to hardcode it in JS.
Here's an example
// Actionscript source
const jsMethodName:String = "someFunction";
const asCallbackName:String = "someCallback";
ExternalInterface.call(jsMethodName+"(document.getElementById("++")"++");");
ExternalInterface.addCallback(asCallbackName,someASfunction);
// Javascript source
function someFunction(flashId, callbackName)
{
var flashContainer = document.getElementById(flashId);
flashContainer["callbackName"]();
}
EDIT: If you really want to get a reference to flash DOM object in someFunction arguments, you may achieve it in a bit tricky way (I would rather not, but just for your interest).
// Actionscript source
const jsMethodName:String = "someFunction";
const asCallbackName:String = "someCallback";
ExternalInterface.addCallback(asCallbackName,someASfunction);
ExternalInterface.call(
"function(){"+
jsMethodName+"("+
"document.getElementById('"+ExternalInterface.objectID+"'),"+
"'"+asCallbackName+"'"+
");"+
"}"
);
// Javascript source
function someFunction(flashContainer, callbackName)
{
flashContainer[callbackName]();
}
This way you inject some JS code from flash into js. It works, but looks messy.