Javascript 如何更改弹出窗口的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14344248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Change a Popup Window's Location
提问by SteveKerr
I have a popup window being opened by my application as follows:
我的应用程序打开了一个弹出窗口,如下所示:
function newPopup(url, windowName) {
window.open(url,windowName,'height=768,width=1366,left=10,top=10,titlebar=no,toolbar=no,menubar=no,location=no,directories=no,status=no');
}
<a href="JavaScript:newPopup('lobby.do', 'lobby');">Enter Lobby</a>
Inside that popup window, I want the user to have the ability to move that popup's window location by clicking a link:
在该弹出窗口中,我希望用户能够通过单击链接来移动该弹出窗口的窗口位置:
<a href="game.do">New Game</a>
However, when click this link in the popup window, the popup is automatically closed and the redirect does not happen. I've tried adding an onClick and using javaScript:window.location, but get the same results. Any idea what could be causing this? I've tested in both Chrome and Firefox.
但是,当在弹出窗口中单击此链接时,弹出窗口会自动关闭并且不会发生重定向。我尝试添加一个 onClick 并使用 javaScript:window.location,但得到了相同的结果。知道是什么原因造成的吗?我已经在 Chrome 和 Firefox 中进行了测试。
回答by Rocket Hazmat
Just set the targetof the link to the popup's name.
只需将target链接的 设置为弹出窗口的名称。
<a href="game.do" target="lobby">New Game</a>
Or save the value returned from window.openand set its .location.
或者保存返回的值window.open并设置它的.location.
function newPopup(url, windowName) {
return window.open(url,windowName,'height=768,width=1366,left=10,top=10,titlebar=no,toolbar=no,menubar=no,location=no,directories=no,status=no');
}
<a onclick="window.lobby=newPopup('lobby.do', 'lobby'); return false;" href="#">Enter Lobby</a>
<a href="window.lobby.location='game.do'; return false;" href="#">New Game</a>
回答by Rachel Gallen
What you need to do to open it in a new window is add target_blankat the end of a htmllink like this:
在新窗口中打开它需要做的是target_blank在html链接的末尾添加,如下所示:
<a href="http://link-to-your-game" target="_blank">Enter Lobby</a>
回答by doniyor
why not to use ajax:
为什么不使用ajax:
<a onclick="ajaxcall_to_newpage()"> click here to update the content </a>
<a onclick="ajaxcall_to_newpage()"> click here to update the content </a>
and your ajax function requesting a new content from server and rendering it to html:
以及您的 ajax 函数从服务器请求新内容并将其呈现为 html:
..
}).done(function(data){
$('#your_div_of_pop_up').text(data);
});
job is done!
工作完成了!
回答by blo0p3r
If I understand correctly what you need is to name your popup. Then you can reference that popup to change the content of it.
如果我理解正确,您需要的是命名您的弹出窗口。然后您可以引用该弹出窗口来更改其内容。
All you need is a bit of JavaScript
你只需要一点点 JavaScript
function createPopup(url) {
newwindow=window.open(url,'testWindow','height=800,width=600');
if (window.focus) {newwindow.focus()}
return false;
}
That can be used like this :
可以这样使用:
<a href="#" onclick="createPopup('http://www.google.com')" target="mine">use google</a>
<a href="#" onclick="createPopup('http://www.stackoverflow.com')" target="mine">use stackoverflow</a>
So from the mainwindow you can control your popup.
因此,您可以从主窗口控制弹出窗口。

