From actionscript to javascript

Convert Actionscript to Javascript

I was just wondering is there anyway to convert Actionscript to Javascript. When I say Actionscript I mean Actionscript 3. I’ve used google swiffy but Actionscript 3 is hardly supported. I’ve also heard of Jangaroo but its not what I want. Even if its a code comparison! Thanks!

3 Answers 3

Javascript and ActionScript (especially AS3) are syntactically similar languages and both are based on the ECMAScript Specification. There are some small differences in the actual code such as:

//Actionscript: var a:String = new PlayerName(); //JavaScript: var a = new PlayerName(); 

This is a demonstration that JavaScript does not have explicit variable type declarations, but this is not the real problem.

What you’re asking goes much further than syntactic incompatibilities, as JS and AS work with completely different APIs. ActionScript has stages, frames and other Flash-based things which do not exist in JavaScript’s environment. JavaScript — usually running in a browser — is used to manipulate documents, DOM nodes and CSS properties.

This means that unless you’re just doing simple function calls and mathematics (without any dependency on the user or their environment) the things your program is doing simply cannot be transferred to another environment. For example, you cannot tell JavaScript to play() or goToAndStop() because there are no frames to play, stop or go to in a HTML document.

Unfortunately, I think what you’re wondering is valid, but the question is almost certainly incorrect. If you have an application created in Flash or any other AS-enabled environment, you probably want to think about porting or re-writing it to the new context.

Читайте также:  Получить текущий день недели php

Источник

Actionscript3 to JavaScript communication: best practices

On a more abstract level then a previous question, in my experience there are 3 ways to call a javascript function on an html page from an embedded .swf using AS3: ExternalInterface, fscommand, and navigateToURL. Let’s compare and contrast these methods (and maybe others I haven’t listed) and talk about the pros and cons of each — right now, ExternalInterface seems like the way to go in terms of flexibility, but is it right for all situations? Are there concrete benefits in terms of execution speed or anything like that? I’m curious — what do we think?

3 Answers 3

ExternalInferface was created to make communication between JS and Flash easier, so it doens’t really make sense to use anything else. Common practice is to check if its available first by evaluating the value of the ExternalInterface.available property before making a call to some JS. This property tells you if the SWF in which you want to call some JS from is inside a container that offers an external interface. In otherwords, if using ExternalInterface will work. If its not available then just use flash.net.sendToUrl. Never use fscommand() as it uses VBScript and can cause conflicts with other VBScript on a page. Additionally, you can only send one argument string with fscommand and have to split it on the JS side.

okay — I like it, that’s what it was made for, the VBScript thing, the single argument thing, and the navigateToURL as an alternative — so, when you check for the availability of ExternalInterface, are you checking on the flash side, or is it somehow checking to see if javascript is there?

Источник

Making ActionScript objects available to JavaScript

JavaScript in the HTML page loaded by an HTMLLoader object can call the classes, objects, and functions defined in the ActionScript execution context using the window.runtime , window.htmlLoader , and window.nativeWindow properties of the HTML page. You can also make ActionScript objects and functions available to JavaScript code by creating references to them within the JavaScript execution context.

A basic example of accessing JavaScript objects from ActionScript

The following example illustrates how to add properties referencing ActionScript objects to the global window object of an HTML page:

var html:HTMLLoader = new HTMLLoader(); var foo:String = «Hello from container SWF.» function helloFromJS(message:String):void < trace("JavaScript says:", message); >var urlReq:URLRequest = new URLRequest(«test.html»); html.addEventListener(Event.COMPLETE, loaded); html.load(urlReq); function loaded(e:Event):void

The HTML content (in a file named test.html) loaded into the HTMLLoader object in the previous example can access the foo property and the helloFromJS() method defined in the parent SWF file:

  function alertFoo()   

When accessing the JavaScript context of a loading document, you can use the htmlDOMInitialize event to create objects early enough in the page construction sequence that any scripts defined in the page can access them. If you wait for the complete event, only scripts in the page that run after the page load event can access the added objects.

Making class definitions available to JavaScript

To make the ActionScript classes of your application available in JavaScript, you can assign the loaded HTML content to the application domain containing the class definitions. The application domain of the JavaScript execution context can be set with the runtimeApplicationDomain property of the HTMLLoader object. To set the application domain to the primary application domain, for example, set runtimeApplicationDomain to ApplicationDomain.currentDomain , as shown in the following code:

html.runtimeApplicationDomain = ApplicationDomain.currentDomain;

Once the runtimeApplicationDomain property is set, the JavaScript context shares class definitions with the assigned domain. To create an instance of a custom class in JavaScript, reference the class definition through the window.runtime property and use the new operator:

var customClassObject = new window.runtime.CustomClass();

The HTML content must be from a compatible security domain. If the HTML content is from a different security domain than that of the application domain you assign, the page uses a default application domain instead. For example, if you load a remote page from the Internet, you could not assign ApplicationDomain.currentDomain as the application domain of the page.

Removing event listeners

When you add JavaScript event listeners to objects outside the current page, including runtime objects, objects in loaded SWF content, and even JavaScript objects running in other pages, you should always remove those event listeners when the page unloads. Otherwise, the event listener dispatches the event to a handler function that no longer exists. If this happens, you will see the following error message: “The application attempted to reference a JavaScript object in an HTML page that is no longer loaded.» Removing unneeded event listeners also lets AIR reclaim the associated memory. For more information, see Removing event listeners in HTML pages that navigate .

Источник

Оцените статью