0

Is that possible to fire Flash-button click-event via javascript ?

it`s my code , and i call fromJS() from javascript and it fires without any problem , but contain of this function :

myButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));

does not work !

package 
{

import flash.external.*;
import flash.net.FileReferenceList;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.system.Security;

public class MultiSelectClass extends MovieClip
{
    private var fileRef:FileReferenceList;

    function MultiSelectClass()
    {
        Security.allowDomain( "*" );
        myButton.addEventListener(MouseEvent.CLICK, myButtonClick);
        addCallbacks();
    }

    private function addCallbacks():void
    {
        if (ExternalInterface.available)
        {
            ExternalInterface.addCallback("sendToFlash", fromJS);
        }
        return;
    }

    function myButtonClick(ev:MouseEvent):void
    {
        fileRef = new FileReferenceList();
        fileRef.browse();
    }

    private function fromJS():void
    {
         myButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
    }

    private function sendToJS():void
    {
        if (ExternalInterface.available)
        {
            ExternalInterface.call("alert","Hello as3");
        }
    }
}
}
6
  • Try putting a trace statement in the myButtonClick function to make sure it's being called. Commented Aug 3, 2012 at 18:01
  • OR, try calling myButtonClick(null) directly from the 'fromJS()' function and forget dispatching the click event all together. Commented Aug 3, 2012 at 18:02
  • Does it work when you actually click the flash button? Commented Aug 3, 2012 at 18:30
  • For security reasons, the file upload functions in Flash Player can only be triggered by user interaction, like clicking a button, in Flash. You will not be able to trigger it from JavaScript, and not by emulating a click dispatching a MouseEvent. This was introduced with Flash Player 10, and broke a lot of file upload solutions back then, like SWFUpload and others (bit-101.com/blog/?p=1382). Commented Aug 4, 2012 at 8:41
  • Hi . about myButtonClick(null) , it does not work ... Does it work when you actually click the flash button? Yes ! and about trace , i have to try and i tell you the result ... thank u so much for replay ... Commented Aug 5, 2012 at 18:04

1 Answer 1

2

You can do this using the ExternalInterface class and adding a callback handler to said function. The flash side would look like this:

if (ExternalInterface.available) {
    ExternalInterface.addCallback("fromJS", fromJS);
}

Then in your javascript, you just call that method from the flashObject.

 mySwfObject.fromJS();

Here is a link to official adobe documentation: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html

-YOUR ISSUES IN THE COMMENTS-

Flash for security reasons will not let you popup the File browser without an actual mouse click, dispatching the mouse event manually does not fool it and there isn't (or at least shouldn't) be a workaround for this. The user will have to actually click your flash button to bring up the file browser (or to full screen a flash app).

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

12 Comments

Thanks, i did it but it does not work ! with ExternalInterface i can call ActionScript function but i can`t fire button click event
i call fromJS() from javascript and it fires without any problem , but contain of this function : myButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK)); does not work !
Your issue then is not with javascript but with your flash code. When you say it's not firing your click event, do you mean that it throws an error, or that your listener isn't receiving the event?
Your code is correct, and should be dispatching the click event as though you'd clicked myButton. You're problem may be in how you're listening for that click. If you post your code for that we can troubleshoot further.
Good though Sanchez157643, I believe flash has some security rules with launching the FileBrowser & FullScreen to make sure it's an actual user click. It's probably unlikely though that having the event argument optional will get around it.
|

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.