如何为新打开的窗口设置ID

how to set an ID to a newly opened window?

本文关键字:窗口 设置 ID 新打开      更新时间:2023-09-26

我有一个窗口,可以从我的javascript代码打开:

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

当满足某个条件时,我想更改该窗口的html地址,即将其重新加载到新位置。

如何为手动打开的窗口设置ID或锚点,以便使用此ID用新位置重新加载该窗口。

使用window.open()时,您可以1)对窗口进行引用,2)声明名称。

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

您可以将窗口存储在一个变量中,然后像通常控制窗口对象一样控制它。

以下代码将打开一个新窗口,4秒钟后重新加载到新位置。

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

如果您想从窗口A更新窗口B的URL,可以执行window.open并提供与第二个参数相同的名称。这将简单地"重新打开"已经打开的窗口,并将其重定向到新的URL。

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

上面的代码将在谷歌打开一个窗口,并立即将其重定向到堆栈溢出

不确定这是否会自动聚焦窗口!

您可以使用以下方式访问打开子窗口的父窗口中的函数。

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