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

2019-07-07 04:49发布

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.

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

5条回答
姐就是有狂的资本
2楼-- · 2019-07-07 05:05

Use

location.href = strquerystring;

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

查看更多
甜甜的少女心
3楼-- · 2019-07-07 05:07

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";
查看更多
萌系小妹纸
4楼-- · 2019-07-07 05:08

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

So in your else clause:

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.

查看更多
SAY GOODBYE
5楼-- · 2019-07-07 05:15

You need to use the name attribute:

window.open("www.youraddress.com","_self");
查看更多
时光不老,我们不散
6楼-- · 2019-07-07 05:15

If you want to change the current URL:

location.replace(strquerystring);
查看更多
登录 后发表回答