2016-01-02 38 views
2

我可以在與父標籤相同的狀態下在JavaScript中打開新標籤頁嗎?如果是,我該怎麼辦?如何創建與父窗口狀態相同的新窗口?

我已經使用了window.open(),但它只是以新的空白狀態打開新選項卡。

+3

「同一州」是什麼意思? –

+0

這意味着每當我打開新標籤時,所有的js變量和父元素的對象都會被加載到子元素中。 –

+2

1)在標籤式瀏覽器(如Chrome)中,每個選項卡都包含自己的窗口對象。標籤之間沒有父母/子女關係。 2)你有什麼更高層次的目標? –

回答

2

您無法準確創建具有相同狀態的窗口。你解釋它的方式,這聽起來像你想要像父母的窗口像一個unix進程fork

在你的情況下可能會有所幫助的是使用窗口上的postMessage方法在窗口之間發送消息對象。從MDN

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

樣品:

/* 
* 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); 

這是一個非常簡單的模式,但是從那裏,你應該能夠發出更復雜的消息。