JavaScript Window Controller

How to I get a reference to a browser window I've opened up and set focus on one of its controls?

This is a pretty common question and I thought I'd share a simple JavaScript window controller class I wrote.  If you launch your windows via this controller, it will maintain a reference to it for further use.  That browser window reference also gives you access to the document object model (DOM) enabling you to set focus on controls or even set their values.  The nice thing about this JavaScript window controller is that if you attempt to launch the same url again, it will not reload the page or launch a new one.  It will check to see if that window/browser tab is already there and set focus to it.  An online demo can be found here: Demo.  The source code can be found below:

    

function WindowController()
{
    this.Windows = new Array();
}

function BrowserTab()
{
    this.ID = '';
    this.WindowReference = null;
}

WindowController.prototype.openWindow = function (title, url, focusControlID)
{

    try
    { 
        if (focusControlID == null) { focusControlID = ''; }

        focusControlID = focusControlID.trim();

        var win = window.open(url);

        win.window.onload = function (e)
        {
  
            win.window.document.title = title;

            if (focusControlID.length <1) { return true; }
            
            var control = win.window.document.getElementById(focusControlID);

            if (control == null) { return true; }
               
            control.focus();
                
        }
 
    }
    catch (e)
    {
        console.log('openWindow ' + e);
    }
}

WindowController.prototype.openBrowserTab = function (browserTabID, url, focusControlID)
{
    var self = this;

    try
    {
        if (focusControlID == null) { focusControlID = ''; }

        focusControlID = focusControlID.trim();

        var count = self.Windows.length;

        if (count > 0)
        {
        
            for (var i = count - 1; i >= 0; --i)
            {

              var browserTab = self.Windows[i];

              if (browserTab == null || browserTab.WindowReference == null)
              {
                self.Windows.splice(i);
                continue;
              }

              if (browserTab.WindowReference.closed)
              {
                self.Windows.splice(i);
                continue;
              }
 
            }
        }

        for (i = 0; i < self.Windows.length; i++)
        {
            var browserTab = self.Windows[i];

            if (browserTab.ID != browserTabID) { continue; }
            
            browserTab.WindowReference.focus();

            if (focusControlID.length < 1) { return true; }
             
            var control = browserTab.WindowReference.window.document.getElementById(focusControlID);

            if (control == null) { return true; }
                
            control.focus();

            return true;
            
        }

        var browserTab = new BrowserTab();

        browserTab.ID = browserTabID;
        browserTab.WindowReference = window.open(url, browserTab.ID);

        browserTab.WindowReference.window.onload = function (e)
        {
  
            browserTab.WindowReference.window.document.title = browserTab.ID;

            if (focusControlID.length < 1) { return true; }
            
            var control = browserTab.WindowReference.window.document.getElementById(focusControlID);

            if (control == null) { return true; }
            
            control.focus();
 
        }

        self.Windows.push(browserTab);

    }
    catch (e)
    {
        console.log('openBrowserTab ' + e);
    }
}
 
By Robbe Morris   Popularity  (811 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP
Here's my most recent course on Pluralsight. I think it has some interesting insight on IT professional job interviews and using words in your resume to influence the questions you'll be asked. Resumes, Job Seeking, and Interviews in context.