Is it possible to use Javascript in flash. For example, as a simple test I am trying to use Javascript's alert method when a button is clicked.
I am using AS3. Is it possible?
Like Marty mentioned, you can use the ExternalInterface class to execute a Javascript. Your options are to execute a function embedded in the (html) page code and create one from scratch. Because alert is a default function you can use its name as the first parameter for the call method (of ExternalInterface) and the string as the 2nd one.
If you provide a single parameter, you might wanna write a function instead to execute (or return) something.
btn.addEventListener(MouseEvent.CLICK, btnClicked);
function btnClicked(e:MouseEvent):void {
ExternalInterface.call("alert","something");
// or
ExternalInterface.call("function(){alert('something');}");
}
No.. Flash uses ActionScript 3.
You can call a JavaScript function on the same page as an embedded SWF using ActionScript's ExternalInterface class, though.
A quick demo on implementing ExternalInterface:
if(ExternalInterface.available)
ExternalInterface.call("alert", "Hello!");
Tip: ExternalInterface calls will not work locally unless you add the location of the project in this security settings panel and check "always allow".
Related
I'm wondering if it is possible to, in Java, detect whether or not an HTML file would open an alert dialog if opened in the browser. Preferably headlessly. For example, a file with the below contents were parsed, it would return true.
<html><script>alert("hey")</script></html>
and the below would return true also
<html><iframe src="javascript:alert(1)" onload="alert(2)"></iframe></html>
but the below would return false because it would not open an alert dialog if it were opened in the browser (because none of the code is syntactically correct, and the part that is isn't in a tag).
<html><script>alert;,(123w)</script>alert(1)</html>
I have thought of a way to approach this problem, but it is flawed. Basically, you see if the stringalert(1) is in the file, etc.
The problem with this is that it wouldn't work in cases where that code isn't inside of script tags or tags that make it execute. An example of where it wouldn't work is: The following would return true, even though it wouldn't actually open a popup <html>alert(1)</html>.
This isn't Android by the way. Appreciate your help!
You will need to not only verify if the Alert function is there but check if the JavaScript function would even run. An example of this is if there is a script with an Alert function inside a function that never runs. The Alert function would be there but it would never run. This would give a false positive. So the in the best case you should run the JavaScript in some way to validate the code and to see if the function would ever run.
As Louis pointed out in the comments Option 2 is better in this case as you will need to account for both the DOM and JavaScript's behaviour as both can change if the Alert function runs and how it runs.
Option 1 : Run the JavaScript with Script Engine
You would need some way of separating the HTML from the JavaScript but once you have that you can do this method.
You can run the JavaScript in Java using ScriptEngine. https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/api.html
If you read the API there is a way to create variables and communicate between your Java Program and the JavaScript you are Running.
To capture the context of the Alert you can create a custom JavaScript function that overwrites the Alert function. Inside this custom function you can send the arguments of the function back to your Java Program.
Option 2 : Headless Browser
You can also try to use a headless browser like JBrowserDriver and as you can see you have an Alert interface with getText as a function. For async issue the headless browser has a default amount of time for waiting for the script to complete. If this default amount is not enough you can use the setScriptTimeout to handle it.
http://machinepublishers.github.io/jBrowserDriver/
I'm trying to get JS in a Wordpress webpage and an Actionscript 2 Flash movie to talk to one another. The attempt is failing miserably. On every attempt to call the function that I've set a callback for, I get "..... is not a function" in the browser error console (I'm using Firefox 20).
Here's how things are set up:
The page is a bit unusual, being a Wordpress page with inline javascript. The main javascript is a jQuery.ready() block of code that loads the flash object (this is done so that GET parameters in the URL can be passed into the flash). Once it's loaded, there's a link with this:
Region A
Meanwhile, the flash object has this in it to make it possible:
import flash.external.ExternalInterface;
System.security.allowDomain("thisdomain.com"); // the domain on which the flash is hosted
ExternalInterface.addCallback("setRegion", null, switchZone); //switchZone is the function's internal name
The flash's container has allowScriptAccess set to "always", and I can confirm that the jQuery statement is hitting the right target. However, when the flash object is debugged, the addCallback returns false— a sign that it's failed. Anyone have any ideas what could be going on?
I met this kind of problem before. To explain this, you may just image your flash file to be a image. Usually, the image in you page will show after the whole page is loaded. For your flash file, in $.ready event, the flash DOM is inserted into your page, but the content of it is loading and the environment of it is not ready yet.
To handle this, you need to register a callback function in your page like this:
window.ping = function () {
$('#fmap')[0].setRegion('regiona');
}
Then in your flash environment, call the ping() registered.
The order of function call is the key point here.
OK, figured it out. First off, the function declaration needed to be above the ExternalInterface.addCallback bit. In addition, once that was done it started throwing a different error, so I had to make a new function... thanks for your help.
I want to implement a JavaScript API to initiate phone calls programmatically and without the need for a native Objective-C implementation.
What I tried so far was something like location.href='tel:12345';. The result was an error with the message "The URL can’t be shown".
My second attempt was to create a hidden link (like call) and click it programmatically. This didn't work either, although the link worked when not hidden and clicked manually by the user.
Is it possible to achieve this with JavaScript only or do I need to delegate to the Objective-C part of the application to initiate the call from there?
Instead of tel:12345 have you tried tel://12345 ?
call
Is your url is correct?
Use window.open() for this:
window.open( 'tel:800-888-1234', '_top' );
You will probably have to write the code to do so natively and create a JavaScript function to be called from within the WebView.
is it possible to call javascript functions inside flash (as3)? How about not in the same domain? Can you provide an example snippet for samedomain and not same domain?
thanks!
Using the ExternalInterface you can communicate with JavaScript from Flash, however only in the window where the Flash application is running.
It is as easy as doing:
ExternalInterface.call("jsFunctionName", argument, argument, ...);
To do the reverse (calling Flash from JavaScript) you do the following first:
ExternalInterface.addCallback("jsFunctionName", callbackFunction);
function callbackFunction(arg:String):void {
trace(arg);
}
And then you can call jsFunctionName("foo") from JavaScript.
See the adobe docs for more info on that.
As for your cross domain, you can't as far as I know, but you may be able to proxy the call via your server.
I have a function defined in AS3 that's gonna be called from client side via JavaScript. The AS3 functions simply print some value on SWF screen when it's called.
It works fine when I set an onclick event handler to a button and call the AS3 function. However I want to call this AS3 function as soon as the page loads. I am using jQuery in the project and I placed the call to the AS3 function inside $(document).ready(), but that gives me the following error in FF2 firebug:
getFlashMovie("my_movie_name").my_as3_function is not a function
Then, I tried calling the by setting an onLoad event handler on the , but that also does not work - produces the same error.
So, my question is, how do I call an AS3 function automatically once page loads? In my project, I need to pass some client side initialization information to the flash once page loads.
You'll need to have your flash call a function in the page to notify it that the Flash is loaded and initialized, then use that as your entrypoint.
In the flash:
ExternalInterface.call('flashReady');
In the page:
<script>
function flashReady() {
..
}
</script>
If you use swfObject to embed your SWF (probably a good idea anyway) then you can use its addDomLoadEvent() function which allows you to do something once the SWF is fully loaded
swfobject.addDomLoadEvent(function() {
$("#swfobject").get(0).inited('you are loaded!');
});
i am not trying to be a wiseguy here but do you test your work on a server?
external interface, addcallback dose not work on local filesystem, and eventually you may have to add:
flash.system.Security.allowDomain('http://localhost');
if you are running on local.
:P
The problem is that the Flash object is not initialized yet when the page finishes loading. It would probably be much safer to perform this initialization from within AS3. If you want to pass values from the HTML page, use flashVars.
I ran into this problem myself a couple of weeks ago. The solution is pretty simple :)
First, you need to put in your DOM a div
<div id="timewriter"><div>
You'll also be using the jQuery Timers plugin to time your loading. After this preparation the things will go very easy.
The following piece of code will go in your $(document).ready();
var movie = getFlashMovie('my_movie_name');
if(movie.PercentLoaded() != 100)
{
$("#timewriter").everyTime(100, function ()
{
if(movie.PercentLoaded() == 100)
{
$("#timewriter").stopTime();
//the movie is loaded, call here your functions; usually this happens if you don't use cache
}
});
}
else
{
//the movie is loaded, call here your functions; usually you get here if you use cache
}
Later edit: be careful that HTML page load doesn't mean the swf was loaded, that happens right after the web page load complete event. Also my solution is based on jQuery javascript library.
Answers by both tweakt and Bogdan are viable. Use tweakt's method if you have access to the Actionscript. Use Bogdan's if you don't. I was looking for an alternative besides polling (when you don't have access to the Actionscript) but I have been unsuccessful in finding one thus far. Events are mentioned here: http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html But noone seems to know how to use them.
For the sake of completion, you would also have to use import flash.external.*; to make everything work.
It seems like the collection of answers offered answers this closest to it's entirety.
As David Hanak said, the flash object cannot be accessed yet because it is initializing, though i disagree that we must rely on flashvars, though I love them.
Tweakt is right, but upon calling the function in the javascript, have that call the javascript function that calls back to your swf; This way we know flash is ready as it sent the first call.