Javascript 如何创建与父窗口状态相同的新窗口?

Javascript 如何创建与父窗口状态相同的新窗口?,javascript,Javascript,我可以在javascript中以与父选项卡相同的状态打开新选项卡吗?如果是,我怎么做 我使用了window.open(),但它只是以新的空白状态打开新选项卡。您无法准确创建具有相同状态的窗口。按照您的解释方式,听起来像是希望将父窗口作为unix进程进行分叉 在您的情况下,可以使用窗口上的postMessage方法在窗口之间发送消息对象 来自mdn的样本: /* * In window A's scripts, with A being on <http://example.com:80

我可以在javascript中以与父选项卡相同的状态打开新选项卡吗?如果是,我怎么做


我使用了window.open(),但它只是以新的空白状态打开新选项卡。

您无法准确创建具有相同状态的窗口。按照您的解释方式,听起来像是希望将父窗口作为unix进程进行分叉

在您的情况下,可以使用窗口上的
postMessage
方法在窗口之间发送消息对象

来自mdn的样本:

/*
 * In window A's scripts, with A being on <http://example.com:8080>:
 */

var popup = window.open(...popup details...);

// When the popup has fully loaded, if not blocked by a popup blocker:

// This does nothing, assuming the window hasn't changed its location.
popup.postMessage("The user is 'bob' and the password is 'secret'",
                  "https://secure.example.net");

// This will successfully queue a message to be sent to the popup, assuming
// the window hasn't changed its location.
popup.postMessage("hello there!", "http://example.org");

function receiveMessage(event)
{
  // Do we trust the sender of this message?  (might be
  // different from what we originally opened, for example).
  if (event.origin !== "http://example.org")
    return;

  // event.source is popup
  // event.data is "hi there yourself!  the secret response is: rheeeeet!"
}
window.addEventListener("message", receiveMessage, false);
/*
*在窗口A的脚本中,打开时:
*/
var popup=window.open(…弹出详细信息…);
//当弹出窗口已完全加载时,如果未被弹出窗口阻止程序阻止:
//假设窗口未更改其位置,则此操作不起任何作用。
popup.postMessage(“用户是‘bob’,密码是‘secret’”,
"https://secure.example.net");
//这将成功地将要发送到弹出窗口的消息排队,假设
//窗口的位置没有改变。
postMessage(“你好!”http://example.org");
函数接收消息(事件)
{
//我们信任此邮件的发件人吗?(可能是)
//例如,与我们最初打开的不同)。
如果(event.origin!==”http://example.org")
返回;
//event.source弹出窗口
//event.data是“你好,你自己!秘密响应是:rheeeeet!”
}
window.addEventListener(“消息”,receiveMessage,false);

这是一个非常简单的模式,但是从这里开始,您应该能够发送更复杂的消息

“相同状态”是什么意思?这意味着每当我打开新选项卡时,父元素的所有js变量和对象都将加载到子元素。1)在选项卡式浏览器(如Chrome)中,每个选项卡都包含自己的窗口对象。选项卡之间没有父/子关系。2) 你们有什么更高层次的目标?那个么当对象被创建时,我可以把它传递给新的标签吗?我想创建一个多用户应用程序,它使用两个不同的窗口进行通信。