JavaScript Get Opened Window from window.Open

By Robbe Morris

Here's a quick tip for getting a reference to an existing opened window that was launched from window.open

They key is to define your window instance globally.  In this sample taken from a Silverlight editor project I did awhile back, we can reference a popup window from inside Silverlight and return data to the Silverlight app from the opened window.

var SilverlightEditor = null;

function SilverlightEditorLaunch(contentID)
{

    // The ispopup variable tells silverlight whether to append window.opener to its Eval() calls.
    
    SilverlightEditorCurrentRecordID = contentID;

    try
    {
    
        if (SilverlightEditor == null)
         {

             // Try to get a reference to an open window named SilverlightEditorPageTitle.  The open windows
            // spawned by this browser session don't die when the parent url changes. By not passing in a url,
            // to start, we can get access to the existing reference.

            SilverlightEditor = window.open('', SilverlightEditorPageTitle, SilverlightEditorPageOptions);

            if (SilverlightEditor.location.href.indexOf(SilverlightEditorPageName, 0) < 0)
            {
                SilverlightEditor.location = SilverlightEditorPageName + "?ispopup=true";
                return;
            }
    
        }
    
        try
        {
           // Attempt to send data to Silverlight via an existing popup window using the window instance SilverlighEditor.
           SilverlightEditor.SilverlightEditorControl.Content.JavaScriptResponse.Send(1, "200", SilverlightEditorLoadRequest());
         }
        catch (e)
        {
           // SilverlightEditor window was closed and didn't notify this window.opener.  So, launch a new instance.
           SilverlightEditor = window.open(SilverlightEditorPageName + "?ispopup=true", SilverlightEditorPageTitle, SilverlightEditorPageOptions);
         }
    
    }
    catch (e) {alert(e); }
    finally { SilverlightEditor.focus();   }
}

JavaScript Get Opened Window from window.Open  (1262 Views)