Actionscript 2.0: Centered Popup Window

At some point we all need a centered popup windows. Here is a useful Actionscript 2.0 function to help get a popup window, that allows for some customization based on input parameters. Check the actual implementation at the bottom. It is also worth mentioning here, that no AS popup script will work in local testing mode. You must upload to a server (or move to a local server directory) for this to function properly.


function winOpen(url:String, winName:String, wide:Number, high:Number, chrome:Boolean, centered:Boolean)
{
//Chrome on window un-centered
if (chrome == true && centered == false)­
{
getURL("javascript:"+winName+"=window.open('"+url+"', '"+winName+"', 'width="+wide+",height="+high+",left=0,top=0,toolbar=1,location=1,scrollbars=1,status=1,resizable=1,fullscreen=no'); "+winName+".focus(); void(0);");
}
//Chromeless un-centered
else if (chrome == false && centered == false)­
{
getURL("javascript:"+winName+"=window.open('"+url+"', '"+winName+"', 'width="+wide+",height="+high+"'); "+winName+".focus(); void(0);");
}
//Chrome on, centered
else if (chrome == true && centered == true)
{
getURL("javascript:"+winName+"=window.open('"+url+"', '"+winName+"', 'width="+wide+",height="+high+",left=0,top=0,toolbar=1,location=1,scrollbars=1,status=1,resizable=1,fullscreen=no'); "+winName+".focus(); screen_height = window.screen.availHeight;screen_width = window.screen.availWidth; left_point = parseInt(screen_width/2)-("+wide+"/2); top_point = parseInt(screen_height/2)-("+high+"/2); setTimeout('"+winName+".moveTo(left_point,top_point)',10); void(0);");
}
//Chromeless, centered (most popular!)
else if (chrome == false && centered == true)
{
getURL("javascript:"+winName+"=window.open('"+url+"', '"+winName+"', 'width="+wide+",height="+high+",left=0,top=0,toolbar=no,location=no,scrollbars=no,status=no,resizable=no,fullscreen=no'); "+winName+".focus(); screen_height = window.screen.availHeight;screen_width = window.screen.availWidth; left_point = parseInt(screen_width/2)-("+wide+"/2); top_point = parseInt(screen_height/2)-("+high+"/2); setTimeout('"+winName+".moveTo(left_point,top_point)',10); void(0);");
}
}

//Sample of application, requires a button with instance name of button_mc on stage
button_mc.onRelease = function()
{
winOpen("http://www.bitconsultants.net/", "MyWindow", 350, 350, true, true);
­}
function winOpen(url:String, winName:String, wide:Number, high:Number, chrome:Boolean, centered:Boolean)
{
    //Chrome on window un-centered
    if (chrome == true && centered == false)­
    {
        getURL("javascript:"+winName+"=window.open('"+url+"', '"+winName+"', 'width="+wide+",height="+high+",left=0,top=0,toolbar=1,location=1,scrollbars=1,status=1,resizable=1,fullscreen=no'); "+winName+".focus(); void(0);");
    }
    //Chromeless un-centered
    else if (chrome == false && centered == false)­
    {
        getURL("javascript:"+winName+"=window.open('"+url+"', '"+winName+"', 'width="+wide+",height="+high+"'); "+winName+".focus(); void(0);");
    }
    //Chrome on, centered
    else if (chrome == true && centered == true)
    {
        getURL("javascript:"+winName+"=window.open('"+url+"', '"+winName+"', 'width="+wide+",height="+high+",left=0,top=0,toolbar=1,location=1,scrollbars=1,status=1,resizable=1,fullscreen=no'); "+winName+".focus(); screen_height = window.screen.availHeight;screen_width = window.screen.availWidth; left_point = parseInt(screen_width/2)-("+wide+"/2); top_point = parseInt(screen_height/2)-("+high+"/2); setTimeout('"+winName+".moveTo(left_point,top_point)',10); void(0);");
    }
    //Chromeless, centered (most popular!)
    else if (chrome == false && centered == true)
    {
        getURL("javascript:"+winName+"=window.open('"+url+"', '"+winName+"', 'width="+wide+",height="+high+",left=0,top=0,toolbar=no,location=no,scrollbars=no,status=no,resizable=no,fullscreen=no'); "+winName+".focus(); screen_height = window.screen.availHeight;screen_width = window.screen.availWidth; left_point = parseInt(screen_width/2)-("+wide+"/2); top_point = parseInt(screen_height/2)-("+high+"/2); setTimeout('"+winName+".moveTo(left_point,top_point)',10); void(0);");
    }
}

//Sample of application, requires a button with instance name of button_mc on stage
button_mc.onRelease = function()
{
    winOpen("http://www.bitconsultants.net/", "MyWindow", 350, 350, true, true);
­}

About the Author

I am a software developer/IT professional helping businesses save money through informed purchase consulting; website development and marketing; and process automation.