Question

I am working on a Flash banner ad built with Actionscript 3. It will be embedded into web pages.

The ad needs to have a close button. When the user clicks the button the ad should disappear.

This needs to be done entirely in the banner, as we have no control over the webpages where it will be embedded.

In Actionscript 2 I used to do this by running this code when the user clicked the button:

unloadMovie(this);

This seems to no longer work in Actionscript 3. What can I do to achieve this functionality in AS3?

Thanks for your help.

Was it helpful?

Solution

For documentation purposes, here are the conclusions Andreyu and I reached regarding the issue: unloading/removing a swf file from within the swf to allow users access to the elements under the swf.

One option is to use ExternalInterface to inject js code to:

  1. get the id/name of the swf as registered with the DOM
  2. remove the swf element from the DOM using the found id/name

In terms of code, this is using a technique described on Zeh Fernando's blog as modified by Andreyu to include the DOM element removal:

// Based on work by Zeh Fernando: http://zehfernando.com/2011/getting-the-swfs-html-objectembed-id-from-within-the-flash-movie-itself/

function getSWFObjectName(): String {
    // Returns the SWF's object name for getElementById

    // Based on https://github.com/millermedeiros/Hasher_AS3_helper/blob/master/dev/src/org/osflash/hasher/Hasher.as

    var js:XML;
    js = <script><![CDATA[
        function(__randomFunction) {
            var check = function(objects){

                    for (var i = 0; i < objects.length; i++){
                        if (typeof(eval("objects["+i+"]." + __randomFunction)) != undefined) {
                            return objects[i].id;
                        }
                    }
                    return undefined;
                };

                return check(document.getElementsByTagName("object")) || check(document.getElementsByTagName("embed"));
        }
    ]]></script>;

    var __randomFunction:String = "checkFunction_" + Math.floor(Math.random() * 99999); // Something random just so it's safer

    ExternalInterface.addCallback(__randomFunction, getSWFObjectName); // The second parameter can be anything, just passing a function that exists

    return ExternalInterface.call(js, __randomFunction);
}

// Function to remove the SWF from the webpage
function destroyEverything(event:MouseEvent): void {

    var js:XML;
    js = <script><![CDATA[
        function(__SWFContext) {

            var element = document.getElementById(__SWFContext);
            element.parentNode.removeChild(element);

        }
    ]]></script>;


    ExternalInterface.call(js, getSWFObjectName());
}


// Add function to click event of button
close_button.addEventListener(MouseEvent.CLICK, destroyEverything);

OTHER TIPS

If unloadMovie was enough for you, you could simply remove everything from the stage:

//In button click handler, call "removeEverything" function
//function onClickClose(e:MouseEvent):void{
//    removeEverything()
//}
//As a result you will get empty stage

function removeEverything():void {
    while (stage.numChildren) {
        stage.removeChildAt(0);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top