javascript 如何在同一页面打开新窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11243067/
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 open a new window in the same page
提问by hh54188
I have a button:
我有一个按钮:
<button>Click</button>
I want click it and open a new window in the same page, I bind the click event an event handle:
我想单击它并在同一页面中打开一个新窗口,我将单击事件绑定到一个事件句柄:
$('button').click(function () {
var newurl="http://" + window.location["host"] + ";
window.open(newurl, '_parent');
})
But is always open in new tab or new window, the second parameter I have try _self
_top
.
I even have try window.location.href(newurl)
但总是在新标签页或新窗口中打开,我尝试的第二个参数_self
_top
。我什至尝试过window.location.href(newurl)
So how can I solve this problem?Does it matter with browser or OS? I view it in Mac OS's firefox and chrome.
那么我该如何解决这个问题呢?它与浏览器或操作系统有关吗?我在 Mac OS 的 firefox 和 chrome 中查看它。
回答by Blaster
Well that's not possible technically, it also depends on browser tab settings, window settings and third party tab manipulation plugins or addons.
好吧,这在技术上是不可能的,它还取决于浏览器选项卡设置、窗口设置和第三方选项卡操作插件或插件。
Update
更新
OP has cleared the confusion of allthrough his comment below his question, this is what you need to set a new url for current window:
OP通过他在问题下方的评论清除了所有人的困惑,这是为当前窗口设置新网址所需的内容:
$('button').click(function () {
var newurl="http://" + window.location["host"];
window.location.href = newurl; // or simply window.location
})
回答by Raab
window.location =newurl;
will open a new window replacing the parent
window.location =newurl;
将打开一个新窗口替换父窗口
回答by Karesh A
Have you tried this?
你试过这个吗?
<script type="text/javascript">
window.open ('test.htm','_self',false)
</script>
回答by jaypeagi
If you mean you want to replace the current page, window.location = newLocation;
will do.
If you want a modal popup, try JQuery UI Dialog.
如果您的意思是要替换当前页面,window.location = newLocation;
就可以了。如果您想要一个模态弹出窗口,请尝试 JQuery UI Dialog。