Search code examples
javascriptflashactionscript-3

POST to opened javascript window and have Flash close the window


I created a Flash application that reads POST data from a form. A user clicks the button, and the data gets posted to the flash app in a new window (_blank). Now takes the data and then spins a wheel to give users a prize. If they don't win a message pops up letting them know they didn't win. If they don't win, clicking the OK button needs to close the browser window.

I've tried a number of solutions and it seems that the only way to get this done is to launch the window with javascript and then use ExternalInterface.call('window.close'); to close the window from within the Flash actionscript (3) because otherwise the window won't close (I've tried just using window.close and window.close() with no luck)

I understand how to launch a new window with javascript, but I don't know how to simultaneously launch and POST data to that window (the Flash application has to have the POST data.

Is this the best solution? Is there a way to close a browser window easier than what I'm describing?

Demo can be viewed here: http://rgwheel.phase-change.org/click.php


Solution

  • Not sure if I get you right :) You want to somehow get POST data from parent window form into flashvars javascript object at your child window?

    If I get you right, it is pretty simple: you can access parent's window data from child window. Just change javascript code at your child window from this:

    var flashvars = {};
    flashvars.spinID="12345";flashvars.prizeTier="1";flashvars.totalSpins="100";var params = {};
    var attributes = {};
    swfobject.embedSWF("rrw_new_Final_ianb.swf", "flashDiv", "775", "819", "10.0.0", false, flashvars, params, attributes);
    

    Into this:

    var flashvars = {};
    // here you get access to the form of the parent window
    var parentForm = window.opener.document.getElementsByTagName('form')[0];
    
    // and here you can populate your flashvars with values from that form's inputs:
    flashvars.spinId = parentForm.spinID.value;
    flashvars.prizeTier = parentForm.prizeTier.value;
    // and so on. Of course, you can do it with any javascript framework
    
    var params = {};
    var attributes = {};
    swfobject.embedSWF("rrw_new_Final_ianb.swf", "flashDiv", "775", "819", "10.0.0", false, flashvars, params, attributes);
    

    Ah, and I beleive you need to use javascript to open new window, not form's target attribute. Like this:

    <form action="/" method="post" onsubmit="window.open()">
    

    You can customize size, position and other properties of the child window, like it is shown here: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml