2013-07-10 77 views
1

我有我從我的javascript代碼打開了一扇窗:如何將ID設置爲新打開的窗口?

window.open('/Item/Article/' + item.ItemID(), 'ItemArticle', 
'resizable=yes,scrollbars=yes,height=' + res.height + ',width=' + res.width + ', 
left=0', null); 

當滿足特定條件我想改變該窗口的HTML地址,即它重新加載到新的位置。

我該如何設置一個ID或一個錨到我手動打開的窗口,以便我可以使用此ID重新加載該窗口的新位置。

+0

嘗試從代碼 – Amit

回答

1

你有可能1)有一個參考你的窗口和2)說明使用window.open()時的名稱。

var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]); 

您可以通過選擇相同的窗口名稱(第二個參數)來修改您的窗口。

var myWindow = window.open("http://google.com","myWindow"); 
// opens SO in the same window 
window.open("http://stackoverflow.com","myWindow"); 
// the following will only work if it's the same domain, else subject to SOP - 
// so in the case of initially opening it with google, this won't work! 
myWindow.document.location.href = "/example/test.html"; 
// closes your window 
myWindow.close(); 

所以你的情況,以下行應該工作:

window.open('newURL','ItemArticle'/* optionally: ,'Parameters'*/); 
0

如果要從窗口A更新窗口B的URL,則可以執行window.open並提供與第二個參數相同的名稱。這將簡單地「重新打開」已打開的窗口,並將其重定向到新的URL。

window.open('http://www.google.com','googleWindow'); 
window.open('http://www.stackoverflow.com','googleWindow'); 

上面的代碼將打開在谷歌的窗口,並立即把它重定向到堆棧溢出

不確定的,如果這個自動聚焦但是窗口!

+0

中刪除'ItemArticle'他希望從父窗口重定向已打開的窗口。 –

0

您可以使用下面的方法來從那裏子窗口被打開訪問父窗口的功能。

window.opener.ParentWindows(); //Here ParentWindows() is userdefined function coded by me in parent windoW¯¯

+0

他想要從父窗口重定向已打開的窗口。 –

0

您可以將窗口存儲在一個變量,然後控制它,就像你通常會控制窗口對象。

以下代碼將打開一個新窗口,並在4秒後重新加載到新位置。

myWindow=window.open('','','width=200,height=100'); 
myWindow.focus(); 
setTimeout(function(){ 
    myWindow.document.location = "http://www.google.com"; 
}, 4000); 
+0

sindenote:'myWindow.document'服從SOP,所以它會失敗的一個不同的域。 – Christoph