将数据传递到我使用window.open()创建的弹出窗口的最快方法?

Bea*_*red 21 javascript

我有javascript代码使用window.open()函数打开一个弹出窗口.我想将数据从父窗口传递到弹出窗口,我似乎无法在google或SO上的任何地方找到优雅或简单的解决方案,但似乎应该支持这种内置于JS中.将数据传递到弹出窗口的最简单方法是什么?

在此先感谢您的帮助!

Dou*_*ner 48

由于安全限制,它超级容易只有当父窗口和弹出是来自同一个域.如果是这种情况,只需使用:

// Store the return of the `open` command in a variable
var newWindow = window.open('http://www.mydomain.com');

// Access it using its variable
newWindow.my_special_setting = "Hello World";
Run Code Online (Sandbox Code Playgroud)

在子(弹出)窗口中,您可以像这样访问该变量:

window.my_special_setting
Run Code Online (Sandbox Code Playgroud)

你有什么具体的想做的吗?

  • 可能必须在父窗口中使用`newWindow.window.myVariable = ...`,在子窗口中使用`myVariable`. (3认同)