How are writes to other windows' variables synchronized?

时间:2017-05-16 09:22:32

标签: javascript dom

When you open a new window with Javascript, you can read and write that window's variables:

var childWindow = window.open("some-url");
childWindow.someVariable = 42;

You can even take a reference to an object in the other window and update it later.

But, how does this work, especially considering Javascript's single thread of execution?

  • Do the windows share a Javascript context?
  • When you write to a variable in the other window, can you be sure that window isn't in the middle of executing a function that uses it?
  • Does the write block the caller until the target window's call stack clears?
  • Is any of this standardized?

1 个答案:

答案 0 :(得分:-1)

Take a look into: https://developer.mozilla.org/en-US/docs/Web/API/Window/open

This function returns:

A reference to the newly created window. If the call failed, it will be null instead. The reference can access properties and methods of the new window provided it complies with Same-origin policy security requirements.

This is exactly the same object as window in the opened page. It works just as a simple object. If you add a property to childWindow it will be available on the windows of opened page. The same with function execution.