Javascript:关闭弹出窗口,打开新窗口和重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5274109/
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
Javascript: Close Popup, Open New Window & Redirect
提问by Andrej
I would like to close the current popup, open a new window (tab) and redirect to a specific link. This works extremly easy with my <a>
links. I simply add "target="_blank""and it works just fine (it doesnt close the pop up actually, but minimizes it and thats fine too).
我想关闭当前弹出窗口,打开一个新窗口(选项卡)并重定向到特定链接。这对我的<a>
链接非常容易。我只需添加“target =“_blank””,它就可以正常工作(它实际上并没有关闭弹出窗口,但将其最小化,这也很好)。
Since I am using some buttons with a onclick function, I want to implent the "target="_blank" in the button somehow... The button looks like this at the moment:
由于我使用了一些带有 onclick 功能的按钮,我想以某种方式在按钮中实现“target =“_blank”......按钮现在看起来像这样:
<input type="button" onclick="parent.window.opener.location='http://google.com'; window.close();">
This works, but the problem is that it redirects in the parent window and doesnt open a new window...
这有效,但问题是它在父窗口中重定向并且没有打开新窗口......
Any1 has an idea how to get the feature of "target="_blank" so that a click on button will open a new window and than redirect instead of redirecting the parent window?
Any1 有一个想法如何获得“target =“_blank”的功能,以便单击按钮将打开一个新窗口而不是重定向而不是重定向父窗口?
回答by JamesHalsall
Why not just use this in your onclick:
为什么不在您的 onclick 中使用它:
<input type="button" onclick="window.open('http://google.com'); window.close();" />
回答by Marcelo
$('#button').click(function(e)
{
e.preventDefault();
window.open('http://www.facebook.com/','_blank');
});
<input type="button" id="button" value="Go facebook">
It works un IExplorer, Firefox, Chrome, Opera and Safari
它适用于 IExplorer、Firefox、Chrome、Opera 和 Safari
回答by marcosfromero
jQuery code:
jQuery代码:
$(function(){
$('#button').click(function() {
window.close();
window.open('http://www.google.com/');
});
});
HTML code:
HTML代码:
<input type="button" id="button" value="click me">
It works OK in Chrome and IE. In Firefox, you must open this code with window.open
.
它在 Chrome 和 IE 中运行正常。在 Firefox 中,您必须使用window.open
.
回答by Naftali aka Neal
Try this:
尝试这个:
<a href='http://google.com' onclick='window.close();' target='_blank'>LINK</a>