2012-02-09 81 views
1

我是JavaScript新手。这是场景;我有一个每10秒更换一次的图像按钮,当我单击它时会在新窗口中打开一个链接,当图像发生变化时,我点击新图像,它的链接会替换同一子窗口中的链接。不过,我希望它在一个全新的窗口中打开链接。我该如何做到这一点呢? (最好在后面的代码谢谢)什么JavaScript代码可以用来在新窗口中打开,而不是我之前打开的窗口

这是我目前使用的JavaScript代码:

Page.ClientScript.RegisterStartupScript(this.GetType(), "open", " window.open(" + site + ",'open_window','myWindow','width=300,height=300,0,status=1,');", true); 
+0

窗口名称应该改变! – sinsedrix 2012-02-09 13:36:35

回答

2

如果您指定“_blank”作为窗口的名称,将始终打开一个新窗口。

1

你必须到窗口给了不同的名称。你的总是被称为相同的:

window.open(adress1, "NewWindow1", "width=300,height=400,left=100,top=200"); 
    window.open(adress2, "NewWindow2", "width=300,height=400,left=100,top=200"); 

请注意,Opera将始终重用第一个窗口。

1

试试这个:

Page.ClientScript.RegisterStartupScript(this.GetType(), "open", " window.open(" + site + ",'open_window222','myWindow','width=300,height=300,0,status=1,');", true); 

请注意open_window222

你必须给不同的标识符!

从MDN

如果名为strWindowName窗口已经存在,则strUrl被 加载到现有的窗口。

2

window.open的第二个参数是窗口的名称。在你的代码中,你正在替换旧窗口,因为你永远不会改变新窗口的名称。

window.open(address1, 'window1', 'myWindow', 'width=300,height=300,0,status=1'); 
window.open(address2, 'window2', 'myWindow', 'width=300,height=300,0,status=1'); 

编辑:前面已经提到,这将是最容易使用_blank作为第二参数中。 _blank将始终创建一个新窗口,这将防止您必须更改任何其他窗口的窗口名称。

window.open(address1, '_blank', 'myWindow', 'width=300,height=300,0,status=1');