15

Is it possible to trigger click event on Flash object via JavaScript?

3
  • 1
    See stackoverflow.com/questions/2653710/… , I think you can make some additions to that code so that it will process the click function (also pass X and Y coordinates) Commented Jun 8, 2012 at 10:39
  • @Cristy I don't think that's the same, I think OP is talking about receiving the click (for file open dialog, my guess) Commented Jun 8, 2012 at 10:40
  • I want to invoke Click action on Flash object using JS. Commented Jun 8, 2012 at 10:43

4 Answers 4

13

Yes and no. You can use the ExternalInterface to simulate click events in your Flash project, provided they don't care about the event source (i.e. human or not).

However, one of the features in Flash that's particularly obtuse about the click event source is the File Selection dialog. It's not possible to trigger that without a real click from a real user (as far as the browser is concerned); this would actually be a potential security risk if possible. If I remember correctly, this was possible before FP 9 and we exploited this behavior for our file uploader. This started to cause issues once they fixed it, but I'm glad they did so :)

The typical way to overcome this limitation is by creating a transparent Flash object and positioning a layer over it that shows a button image; the click event will eventually hit the Flash object and trigger a user click event.

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

4 Comments

why not disable only that behavior and not wipe out the whole thing.
This is incorrect. As noted below, flash has an externalinterface where you can define functions that javascript can call. Your file selection dialog is still totally feasible, however you have to control the .fla yourself. Clicking on someone else's .fla that you have not written an externalinterface method for is probably impossible with javascript. (however, there are plenty of clicker programs that can send clicks to your browser and it will interpret them as normal clicks)
@SethCaldwell Yes, you're right about the ExternalInterface, I forgot to mention that. But from our own experience, that still doesn't allow you to launch the file picker from within Flash itself ... of course, nowadays this is becoming less of an issue what with the advances of JavaScript in modern browsers ;-)
This link is now rotten
2

I never tested this, so it is just thought

Note please note that you should have a certain div which will capture the clicks.

In Actionscript To simulate a click event, it is possible to use.

 element.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));

So why not creating an external function that will dispatch the event once receiving the invoke from JavaScript using externalinterface? Of course this is very rough but it may well work.

Actionscript:

import flash.external.*;

function simulateButtonClick() 
{ 
// Here goes your code
} 
ExternalInterface.addCallback("invokeSimulateButtonClick", simulateButtonClick); 

Javascript:

 ExternalInterface.addCallback("invokeSimulateButtonClick", YOUR_VARIABLES);

Comments

1

It is possible with Flash ExternalInterface, but you need to write some ActionScript in Flash (.fla) first.

Some references -

Using ExternalInterface in Flash

http://www.actionscript.org/resources/articles/638/1/Basics-of-using-the-ExternalInterface/Page1.html

http://www.adobe.com/devnet/flash/articles/external_interface.html

Comments

0

No you can't capture clicks on embedded elements, unless it's behind a div or something.

Comments

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.