The browser window.open method provides a way to access open windows by name. For example,<h3>window A:</h3>
This will open a blank window B with window.name == 'myWindowName'. Then,<h3>window C:</h3>
This will open example.com in window B.<h2>The problem:</h2>
Rather than creating a new window with name == 'myWindowName', how can I set the name of an already opened window so that it can be accessed by other windows using window.open? Using Chrome the following does not work:<h3>1. open the following html in the target window</h3>
<!DOCTYPE html> <html> <head> <script>window.name='myWindowName'</script> </head> <body> target window </body> </html>
executing window.name in the target window now produces 'myWindowName'<h3>2. execute the following js from the console of another window</h3>
The code above opens example.com in a new window (also with window.name 'myWindowName') rather than the target window.<h3>edit:</h3>
for some reason, in chrome, setting the name in the target window will work if no content is loaded into the window, but once content is loaded setting window.name no longer affects the window.open of other windows.Answer1:
As suggested in the comments above, in order to target a window by name using the window.open method, the target window must have the same origin AND have a common opener.
chrome test:<h3>1. open new window example.com (or any site)</h3>
window.name = 'target' window.was_open = true<h3>2. open new window example.com (or any site)</h3>
w = window.open('', 'target') w.was_open //undefined
It is unknown why the same test works when the js is executed in a window console without loading content first (like example.com).
A common window cannot be targeted from multiple origins, or windows with different openers. For example, window.open cannot be used by a bookmarklet to
postMessage() to a common window.