Javascript window.open() 应该在同一个选项卡中打开链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28164479/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:18:40  来源:igfitidea点击:

window.open() should open the link in same tab

javascripthtmlwindow.open

提问by user2897174

I am very new to javascript, and written a program that will open a querystring as link.
I used window.open() , but the link is opening in new tab,
I want to open this link in the same tab.
The code is below.

我对 javascript 很陌生,并编写了一个程序,该程序将打开一个查询字符串作为链接。
我使用了 window.open() ,但链接在新标签
中打开,我想在同一个标​​签中打开这个链接。
代码如下。

var strquerystring;  
if(fromvalue==""||tovalue==""){  
  alert('kindly fill all the details');
}else{
  window.open(strquerystring);
}

回答by progsource

Use

location.href = strquerystring;

Instead of window.open. It will then change the current URL.

而不是 window.open。然后它将更改当前 URL。

回答by ozil

You need to use the name attribute:

您需要使用 name 属性:

window.open("www.youraddress.com","_self");

回答by taesu

Use either of these:

使用以下任一方法:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

回答by ioseph

If you want to change the current URL:

如果要更改当前 URL:

location.replace(strquerystring);

回答by Erik5388

Instead of asking the browser to open a new window. Try asking the browser to open a new location.

而不是要求浏览器打开一个新窗口。尝试让浏览器打开一个新位置。

So in your else clause:

所以在你的 else 子句中:

window.location = (window.location + strquerystring);

window.location = (window.location + strquerystring);

This will tell the browser to navigate to the location given. Instead of opening a new window. Thus keeping you in the same "tab"

这将告诉浏览器导航到给定的位置。而不是打开一个新窗口。从而让您保持在同一个“标签”中

Hope that helps.

希望有帮助。