2

i have this javascript code:

<script type="text/javascript">
$(document).ready(function() {  
$("body").append('<div id="ajaxBusy" class="ajaxBusy"></div>');
</script>

My code works fine so far but i thought to load this javascript code inside an AS3.

Is there any possibility to make it happen? To write inline javascript code inside my flash; I found this code but i dont know how to implement

import flash.external.ExternalInterface;

var someVarInAS : String = 'foo';
var someOtherVarInAS : int = 10;
var jsXML : XML = 
<script type="text/javascript">
    var someVarInJS = '{$("body").append('<div id="ajaxBusy" class="ajaxBusy"></div>');}';
    var someOtherVarInJS = '{$('#login').fadeIn();}';
    <![CDATA[       
        alert( 'this comes from flash: ' + someVarInJS + ', ' + someOtherVarInJS );
    ]]>
</script>;


ExternalInterface.call( "function js_" + ( new Date().getTime() ) + "(){ " + jsXML + " }" );

Can anyone help me to do this?

2
  • What logic are you trying to send from flash to the page? ExternalInterface provides a way to communicate back and forth between javascript and actionscript, but a better way of handling this would be to use ExternalInterface to call javascript functions that handle that actual javascript actions as opposed to injecting javascript directly from flash. Commented Jun 26, 2013 at 20:04
  • @MSost can you please provide me a demo code? I mean how is it possible when i am loading my flash banner to my web page it runs an external js file to do append a div to my body of the page! Commented Jun 26, 2013 at 20:10

1 Answer 1

2

Here's a link to an example to show how ExternalInterface works

https://dl.dropboxusercontent.com/u/15551758/eitest.zip

What's happening is that once the Flash object is loaded on the page, it uses

ExtenalInterface.call();

to call a javascript function that's been registered on the page that the swf is on. In the case of the example:

ExternalInterface.call('toJS', 'flash text');

calls the javascript function toJS and sends one argument, a string "flash text".

The opposite direction is supported as well. Calling a function on the flash object and passing in arguments will send them to flash. You register ExternalInterface callbacks with:

ExternalInterface.addCallback(callback_name, flash_function_to_call);

In the example, we register in flash a callback:

ExternalInterface.addCallback("fromJS", this.fromJS);

that listens for an event from javascript called fromJS and calls an internal function fromJS. For the example, I've added a textbox on the stage to visualize the data coming in from javascript.

Please let me know if this answers your question or if you need more explanation.

Edit:

If you must inject javascript, you can pass in a function that does the injection as the first parameter of ExternalInterface.call:

var inject:String = "function(){var body = document.getElementsByTagName('body')[0], testNode = document.createElement('div'); testNode.innerHTML = 'This is a test'; body.appendChild(testNode);}";
ExternalInterface.call(inject);

But I'd recommend against it. Keeping languages separate will lead to more concise and easier to debug code.

Sign up to request clarification or add additional context in comments.

4 Comments

it works great @Msot! I have one more wuestion please.. how is it possible to run a javascript code inside AS3? I mean is it possible to append a div to my <body> without load external files?
@IreneT. You can pass a javascript function defined in an AS3 string as the first argument to ExternalInterface.call("function(){var newDiv = document.createElement('div'); document.appendChild(newDiv);}", null); I believe, this way you could dynamically build the javascript function using AS3
@shaunhusain and if it works i want only to give an id name to this div
Ah the joy of writing code that writes code that builds a DOM. This page should help javascriptkit.com/dhtmltutors/domattribute.shtml you can setAttribute on the newDiv after you create it with newDiv.setAttribute("id", "mycustomid"); basically at this point it's just an exercise in javascript. Best of luck and happy hacking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.