admin管理员组

文章数量:1399161

The browser window.open method provides a way to access open windows by name. For example,

window A:

window.open('','myWindowName')

This will open a blank window B with window.name == 'myWindowName'. Then,

window C:

window.open('', 'myWindowName')

This will open example in window B.

The problem:

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:

1. open the following html in the target window

<!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'

2. execute the following js from the console of another window

window.open('', 'myWindowName')

The code above opens example in a new window (also with window.name 'myWindowName') rather than the target window.

edit:

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.

The browser window.open method provides a way to access open windows by name. For example,

window A:

window.open('','myWindowName')

This will open a blank window B with window.name == 'myWindowName'. Then,

window C:

window.open('http://example.', 'myWindowName')

This will open example. in window B.

The problem:

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:

1. open the following html in the target window

<!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'

2. execute the following js from the console of another window

window.open('http://example.', 'myWindowName')

The code above opens example. in a new window (also with window.name 'myWindowName') rather than the target window.

edit:

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.

Share Improve this question edited Nov 6, 2013 at 21:53 Nathan Buesgens asked Nov 6, 2013 at 20:55 Nathan BuesgensNathan Buesgens 1,4451 gold badge16 silver badges30 bronze badges 8
  • 1 What browser are you using? I just tried this in Chrome and it worked as you would expect: two tabs, window.name = 'window1'; in one; window.open('http://example.', 'window1'); in the other opens "example." in the other tab, not a new tab. This also worked with separate windows instead of tabs. – Thomas Upton Commented Nov 6, 2013 at 21:09
  • Seems to work in firefox. – James Montagne Commented Nov 6, 2013 at 21:14
  • 1 Works. jsfiddle/C5pFg – Comfortably Numb Commented Nov 6, 2013 at 21:34
  • when executing all js from blank chrome consoles the issue does not occur. thanks for pointing that out, sorry my testing didn't turn that up. Will edit the question so the problem is easier to reproduce. – Nathan Buesgens Commented Nov 6, 2013 at 21:42
  • 1 Security reasons. Once the window left your domain, you lose any control over it and can't read or set any of its properties, same like with <iframe>. (except maybe closing it) – user447356 Commented Nov 6, 2013 at 21:57
 |  Show 3 more ments

1 Answer 1

Reset to default 2

As suggested in the ments 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 mon opener.

chrome test:

1. open new window example. (or any site)

window.name = 'target'
window.was_open = true

2. open new window example. (or any site)

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.).

A mon 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 mon window.

本文标签: javascriptusing windowopen with windownameStack Overflow