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 = window.open(null,”some existing window name if you know it”);

//get handle to self (current window)
var selfWinHdl = window.open(null,window.name);

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 “window.name” 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 window.open().

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:

someWinHdl.close();

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).
  • window.name 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 window.name 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 window.name (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 window.open() and storing the return value. However, sometimes one might not have stored that value in the original window.open() 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).
Advertisements

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.open(“”, “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=”window.name = ‘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 window.name = ‘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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Seek Nuance

Python, technology, Seattle, careers, life, et cetera...

TELLURIUM

New Era of Test Automation

Der Flounder

Seldom updated, occasionally insightful.

The 4T - Trail, Tram, Trolley, Train

Exploring Portland with the 4T

Midnight Musings

Thoughts on making art

Automation Guide

The More You Learn The More You Play...!

The Performance Engineer

Code.Test.Tune.Optimize.

humblesoftwaredev

Thoughts related to software development

Yi Wang's Tech Notes

A blog ported from http://cxwangyi.blogspot.com

Appium Tutorial

Technical…..Practical…..Theoretically Interesting

LinuxMeerkat

I swear! Meerkats can do Linux

PacketsDropped

Requeuing the packets dropped in my memory.

Two cents of software value

Writing. Training. Consulting.

@akumar overflow

wisdom exceeding 140 chars.

Lazy Programmer's Shortcut

Java, J2EE, Spring, OOAD, DDD & LIFE! .......all in one :)

Testing Mobile Apps

www.SoftwareTestingStudio.com

Photofocus

education and inspiration for visual storytellers

%d bloggers like this: