540

I want to open a link in the same window and in the same tab that contains the page with the link.

When I try to open a link by using window.open, then it opens in new tab—not in the same tab in the same window.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
user1065055
  • 5,471
  • 3
  • 15
  • 4
  • You might want to check out this [post](https://stackoverflow.com/questions/18470097/difference-between-self-top-and-parent-in-the-anchor-tag-target-attribute) to understand differences between the proposed methods below, like `_self` and `_top` which "look-alike". – My Work May 18 '21 at 09:58

15 Answers15

906

You need to use the name attribute:

window.open("https://www.youraddress.com","_self")

Edit: Url should be prepended with protocol. Without it tries to open relative url. Tested in Chrome 59, Firefox 54 and IE 11.

Anton Sutarmin
  • 807
  • 8
  • 15
vdbuilder
  • 12,254
  • 2
  • 25
  • 29
  • 2
    The second argument is just a name to the new window, like the attribute `target=` of tag `a`. In fact, you can name your window whatever you like. All what you need is set it different value, so that it won't open in the same window or tab. – ijse Dec 10 '11 at 14:57
  • 15
    @ijse Actually, there are a few special names, one of which is '_self' which refers to the win/tab the code is running in. ;) – vdbuilder Jun 05 '13 at 00:41
  • 5
    '_self' is not specified in the MDN [https://developer.mozilla.org/en-US/docs/Web/API/Window/open] docs on window.open(). A more cross-browser solution is to use location.replace(). – Bryan Rayner Sep 11 '15 at 19:04
  • 1
    The MDN link in the comment above is auto-linking to a 404. Link is https://developer.mozilla.org/en-US/docs/Web/API/Window/open – groovecoder Sep 29 '15 at 20:42
  • `_self` is mentioned in section **5.1.6 Browsing context names** of the *HTML5 W3C Recommendation 28 October 2014* at: http://www.w3.org/TR/html/browsers.html#browsing-context-names (but `window.location` is still cleaner). – Dem Pilafian Nov 12 '15 at 07:48
  • $window("www.address.com", "_self") - works also in angular way – FrenkyB Nov 19 '16 at 13:06
  • Its not working, Well don't know the reason. I tried `window.location.href="....."` – Qazi Mar 09 '17 at 04:10
  • @Qazi Yah. None of these solutions work for me either x). I actually WANT to open a file relative to the one i open from. Maybe firefox doesnt understand it? cleaning cashe or something needed? without the "_self" part it opens but in new window xP I did try "_blank" as per: https://support.mozilla.org/en-US/questions/1165910 and now it works. – brat Aug 22 '18 at 22:33
  • does it have to be "_self"? I tried it with other string names like 'testabcd' and it works –  Aug 08 '20 at 23:59
200

Use this:

location.href = "http://example.com";
Dave L.
  • 9,595
  • 7
  • 43
  • 69
  • 22
    this is preferred to window.open (per http://stackoverflow.com/questions/4813879/window-open-target-self-v-window-location-href) – Harry Jul 25 '14 at 10:41
  • airbnb linter does not like location.herf. mentioning window at the beginning is the must. – Kick Buttowski Aug 24 '20 at 21:14
110

In order to ensure that the link is opened in the same tab, you should use window.location.replace()

See the example below:

window.location.replace("http://www.w3schools.com");

Source: http://www.w3schools.com/jsref/met_loc_replace.asp

andromeda
  • 4,433
  • 5
  • 32
  • 42
  • 14
    It doesn't preserve the browsing history, we should not use it. instead try window.open("http://www.google.com","_top") reference link https://www.geeksforgeeks.org/open-a-link-without-clicking-on-it-using-javascript/ – shyam_ Jul 03 '19 at 16:03
  • 2
    I am using this as part of an internal addon for internal pages and it's perfect. Browser history isn't really an issue as it's for automation and those using it have no business going to any of the pages manually. Thanks for the snippet! – kevin walker Dec 12 '20 at 01:37
48

You can have it go to the same page without specifying the url:

window.open('?','_self');
Tom Berthold
  • 552
  • 4
  • 4
22

If you have your pages inside "frame" then "Window.open('logout.aspx','_self')"

will be redirected inside same frame. So by using

"Window.open('logout.aspx','_top')"

we can load the page as new request.

Magesh
  • 393
  • 1
  • 3
  • 11
12

One of the most prominent javascript features is to fire onclick handlers on the fly. I found following mechanism more reliable than using location.href='' or location.reload() or window.open:

// this function can fire onclick handler for any DOM-Element
function fireClickEvent(element) {
    var evt = new window.MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true
    });

    element.dispatchEvent(evt);
}

// this function will setup a virtual anchor element
// and fire click handler to open new URL in the same room
// it works better than location.href=something or location.reload()
function openNewURLInTheSameWindow(targetURL) {
    var a = document.createElement('a');
    a.href = targetURL;
    fireClickEvent(a);
}

Above code is also helpful to open new tab/window and bypassing all pop-up blockers!!! E.g.

function openNewTabOrNewWindow(targetURL) {
    var a = document.createElement('a');
    a.href = targetURL;

    a.target = '_blank'; // now it will open new tab/window and bypass any popup blocker!

    fireClickEvent(a);
}
Muaz Khan
  • 7,138
  • 9
  • 41
  • 77
12

Open another url like a click in link

window.location.href = "http://example.com";
DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
11

Do you have to use window.open? What about using window.location="http://example.com"?

Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52
6

window.open(url, wndname, params), it has three arguments. if you don't want it open in the same window, just set a different wndname. such as :

window.open(url1, "name1", params); // this open one window or tab
window.open(url1, "name2", params); // though url is same, but it'll open in another window(tab).

Here is the details about window.open(), you can trust it!
https://developer.mozilla.org/en/DOM/window.open

have a try ~~

ijse
  • 2,998
  • 1
  • 19
  • 25
  • 8
    The question is very clearly about wanting to open in the ***same window*** and ***same tab***! – SNag Jun 12 '13 at 12:05
5

open url in the current tab page using _self

const autoOpenAlink = (url = ``) => {
  window.open(url, "open testing page in a same tab page");
}
<a
 href="https://cdn.xgqfrms.xyz/index.html"
 target="_self"
 onclick="autoOpenAlink('https://cdn.xgqfrms.xyz/index.html')">
   open url in the current tab page using `_self`
</a>

open url in a new tab page using _blank

const autoOpenAlink = (url = ``) => {
  window.open(url, "open testing page in a new tab page");
}

// ❌  The error is caused by a `StackOverflow` limitation
// js:18 Blocked opening 'https://cdn.xgqfrms.xyz/index.html' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.
<a
 href="https://cdn.xgqfrms.xyz/index.html"
 target="_blank"
 onclick="autoOpenAlink('https://cdn.xgqfrms.xyz/index.html')">
   open url in a new tab page using `_blank`
</a>

refs

According to MDN's docs, you just need to give one name of the new window/tab.

https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Syntax

xgqfrms
  • 10,077
  • 1
  • 69
  • 68
  • You can’t reliably know the name of the current window. Better to follow the advice from [this 2011 answer](https://stackoverflow.com/a/8454535/19068) and use the special _self name. – Quentin Apr 29 '19 at 06:55
  • Your first example is just wrong. _blank is, explicitly, an unnamed **new** window or tab. – Quentin Apr 29 '19 at 06:55
  • not at all! if I set _self, it will open in current page which is bad for me, I just need one new page. – xgqfrms Apr 30 '19 at 07:39
  • 4
    The question isn't about what **you** need! If you want to answer a question about what you need, then find a question asking for that. – Quentin Apr 30 '19 at 08:22
2

With html 5 you can use history API.

history.pushState({
  prevUrl: window.location.href

}, 'Next page', 'http://localhost/x/next_page');
history.go();

Then on the next page you can access state object like so

let url = history.state.prevUrl;
if (url) {
  console.log('user come from: '+ url)
}
Adi Prasetyo
  • 1,006
  • 1
  • 15
  • 41
2

Exactly like this window.open("www.youraddress.com","_self")

thanglv
  • 39
  • 6
1

Thats pretty easy. Open first window as window.open(url, <tabNmae>)

Example: window.open("abc.com",'myTab')

and for next all window.open, use same tab name instead of _self, _parent etc.

Sarjan Desai
  • 3,683
  • 2
  • 19
  • 32
Samit
  • 74
  • 6
0
   Just Try in button.

   <button onclick="location.reload();location.href='url_name'" 
   id="myButton" class="btn request-callback" >Explore More</button>

   Using href 

  <a href="#" class="know_how" onclick="location.reload();location.href='url_name'">Know More</a> 
Siddharth Shukla
  • 1,063
  • 15
  • 14
  • 1
    It make no sense at all to trigger a reload of the current page and then cancel it in the next statement by loading a new page. – Quentin Apr 29 '19 at 06:56
0

You can do

window.close();
window.open("index.html");

and it worked successfully on my website.

I-am-developer-9
  • 434
  • 1
  • 5
  • 13