Javascript: Close Popup, Open New Window & Redirect

这一生的挚爱 提交于 2019-12-18 13:32:08

问题


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).

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:

<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?


回答1:


Why not just use this in your onclick:

<input type="button" onclick="window.open('http://google.com'); window.close();" />



回答2:


$('#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




回答3:


jQuery code:

$(function(){
    $('#button').click(function() {
        window.close();
        window.open('http://www.google.com/'); 
    });
});

HTML code:

<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.




回答4:


Try this:

<a href='http://google.com' onclick='window.close();' target='_blank'>LINK</a>


来源:https://stackoverflow.com/questions/5274109/javascript-close-popup-open-new-window-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!