Getting window handle on existing windows in javascript by use of window name

9 Nov

I came across this recently. Don’t recall if I found the basis for technique off the web or not (think I did, forgot the source URL), but adapted it for use in javascript code.

In rare cases, could also be used for Selenium/WebDriver, if you’re not using their built in window handling APIs.

//get handle to some existing window
var someWinHdl =,”some existing window name if you know it”);

//get handle to self (current window)
var selfWinHdl =,;

granted getting the current window handle this way isn’t all that useful though since you could also just use “window.self”. But it is useful to gain access to an existing window if you know it’s “” value.

This same technique can also be applied for opening new windows to keep a handle on the newly opened window, just provide a real URL instead of null to

With this window handle in place you could access it like any window object such as closing it, using the stored handle variable example above:


NOTE – update 7/8/14:

For this technique, the code will only work under these conditions:

  • will only work for current window & any windows that were opened from current window. That means that if window A opened window B, then window B won’t be able to access window A with this technique, even if you knew window A’s name. Or if window B was opened separately/manually by the user as separate tab/window and it somehow has a name, window A won’t be able to access it. This is likely from browser security sandboxing. You can only access windows you control (e.g. current window and any that you opened via javascript from current window).
  • needs to be defined with a value (in the case of getting handle to self/current window) or the window name to another window (opened by current window) needs to be a valid defined window name, otherwise, it will just open a blank window and you won’t get a reference to the (correct) window handle. For current window or self, you can arbitrarily set the value (e.g. to “testing123”) if it is undefined, and then you will be able to use the code above. For other windows currently open, we assume those windows were opened with a defined (or that it is defined at some point) so that you can reference it. If the name is undefined (anonymous), then this code won’t work against other windows that were previously opened by the current window.
  • The code here is generally not that helpful in regards to windows already opened since you get the window handle when opening the window with and storing the return value. However, sometimes one might not have stored that value in the original call, or for testing purposes, you don’t have that handle but you do have the window name. As such, you can make a repeat call with the presented code to retrieve the window handle without changing the current location/URL of that window (e.g. don’t cause it to navigate away or page refresh).

3 Responses to “Getting window handle on existing windows in javascript by use of window name”

  1. Chaitanya July 8, 2014 at 7:33 pm #

    I cant get a reference to an open tab which is in the same session of my website, having a different url.“”, “window name”) is giving me a blank window with no info. Is there anything that prevents it from getting the reference?

    Note: I’m setting the window name “onload” using javascript. Not from some other window.

    • autumnator July 8, 2014 at 11:26 pm #

      I’ve updated the blog post with additional info. Hope that helps.

      For your particular scenario, where/when is the target open tab/window opened? Separately/manually or from the current window via javascript?

      Which window are you setting name for? And you’re setting it with the onload event in javascript in the HTML body tag? As in …body onload=” = ‘my name’;”…? Or do you mean the window name itself is set to “onload” as the name?

      • Chaitanya July 8, 2014 at 11:50 pm #

        I’m setting = ‘some name from server’ , on an onload javascript event. So all the tabs opened in this session will get different window names. Then I’m trying to show a list of open tabs(in all tabs) and change focus when user clicks on them.

        So every window sets its own name. and the other window should be able to access them by window name. From the post you updated I guess its not possible.

        But, I have made it work earlier. I was using ajax loaded content and had same url on all the tabs.

        Now I’m using url hashing, and have different urls on all tabs. In this case the window reference is not being saved for anything other than itself.

        Please let me know if you can think of something that will work along these lines.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Tea in Eighteenth-Century Britain

History of Tea Project at Queen Mary University of London

Phimosis - A Simple Cure (That is working for me)

No nonsense, no adverts, no sales pitch, just honest information.

Glass Onion Blog

Cheat sheets, post-its and random notes from the desk of a programmer

Abode QA

A Hub For Testing Minds...

The Test Therapist

Performance & Security Testing Blog


a programmer's hub

Let's Not Crash and Burn

it makes your brain tingle


“Incinerate Ignorance”

Anastasia Writes

politics, engineering, parenting, relevant things over coffee.

One Software Tester

Trying To Make Sense Of The World, One Test At A Time

the morning paper

a random walk through Computer Science research, by Adrian Colyer

RoboSim (Robot Simulator)

Visualize and Simulate the Robotics concepts such as Localization, Path Planning, P.I.D Controller


open notebook

a happy knockout mouse.

my journey into computer science

Perl 6 Advent Calendar

Something cool about Perl 6 every day


Inspire and spread the power of collaboration

Niraj Bhatt - Architect's Blog

Ruminations on .NET, Architecture & Design

Pete Zybrick

Bell Labs to Big Data

%d bloggers like this: