title: window.focus() not working in Google Chrome
body: <p>Just wondering if Google Chrome is going to support <code>window.focus()</code> at some point. When I mean support, I mean have it work. The call to it doesn't fail, it just doesn't do anything. All other major browsers do not have this problem: FireFox, IE6-IE8 and Safari.</p>
  
  <p>I have a client-side class for managing browser windows. When I first create a window the window comes into focus, but subsequent attempts to bring focus to the window do not work.</p>
  
  <p>From what I can tell, this appears to be a security feature to avoid annoying pop-ups and it does not appear to be a WebKit issue as it works in Safari.</p>
  
  <p>I know one idea someone brought forward was to close the window then reopen it, but this is a horrible solution. Googling shows that I do not appear to be the only person frustrated with this.</p>
  
  <p>And just to be 100% clear, I mean new windows, not tabs (tabs cannot be focused from what I've read) and all the windows being opened are in the same domain.</p>
  
  <p>Any ideas, workarounds aside from the bad one I mention above?</p>
  
  <p>There is a bug logged on the Chromium project about this, check it out <a href="http://code.google.com/p/chromium/issues/detail?id=1383&amp;can=1&amp;q=window.focus%20type%3aBug&amp;colspec=ID%20Stars%20Pri%20Area%20Type%20Status%20Summary%20Modified%20Owner" rel="noreferrer">here</a>. Thanks for posting that <a href="https://stackoverflow.com/users/16511/rich-bradshaw">Rich</a>.</p>
  
  <pre><code>MyCompany = { UI: {} }; // Put this here if you want to test the code. I create these namespaces elsewhere in code.
  
  MyCompany.UI.Window = new function() {
      // Private fields
      var that = this;
      var windowHandles = {};
  
      // Public Members
      this.windowExists = function(windowTarget) {
          return windowTarget &amp;&amp; windowHandles[windowTarget] &amp;&amp; !windowHandles[windowTarget].closed;
      }
  
      this.open = function(url, windowTarget, windowProperties) {
          // See if we have a window handle and if it's closed or not.
          if (that.windowExists(windowTarget)) {
  
              // We still have our window object so let's check if the URLs is the same as the one we're trying to load.
              var currentLocation = windowHandles[windowTarget].location;
  
              if (
                  (
                      /^http(?:s?):/.test(url) &amp;&amp; currentLocation.href !== url
                  )
                      ||
                  (
                      // This check is required because the URL might be the same, but absolute,
                      // e.g. /Default.aspx ... instead of http://localhost/Default.aspx ...
                      !/^http(?:s?):/.test(url) &amp;&amp;
                      (currentLocation.pathname + currentLocation.search + currentLocation.hash) !== url
                  )
              ) {
                  // Not the same URL, so load the new one.
                  windowHandles[windowTarget].location = url;
              }
  
              // Give focus to the window. This works in IE 6/7/8, FireFox, Safari but not Chrome.
              // Well in Chrome it works the first time, but subsequent focus attempts fail,. I believe this is a security feature in Chrome to avoid annoying popups.
              windowHandles[windowTarget].focus();
          }
          else
          {
              // Need to do this so that tabbed browsers (pretty much all browsers except IE6) actually open a new window
              // as opposed to a tab. By specifying at least one window property, we're guaranteed to have a new window created instead
              // of a tab.
              windowProperties = windowProperties || 'menubar=yes,location=yes,width=700, height=400, scrollbars=yes, resizable= yes';
              windowTarget = windowTarget || "_blank";
  
              // Create a new window.
              var windowHandle = windowProperties ? window.open(url, windowTarget, windowProperties) : window.open(url, windowTarget);
  
              if (null === windowHandle) {
                  alert("You have a popup blocker enabled. Please allow popups for " + location.protocol + "//" + location.host);
              }
              else {
                  if ("_blank" !== windowTarget) {
                      // Store the window handle for reuse if a handle was specified.
                      windowHandles[windowTarget] = windowHandle;
                      windowHandles[windowTarget].focus();
                  }
              }
          }
      }
  }
  </code></pre>