[javascript] Open URL in same window and in same tab

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.

This question is related to javascript html hyperlink href

The answer is


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.


As MDN's refs says, just need give a name of the new window/tab.

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

open in the current tab page using _self

<a
 href="url"
 target="_self">
   open
</a>
const autoOpenAlink = (url = ``) => {
  window.open(url, "open testing page in the same tab page");
}

open in a new tab page using _blank

vue demo

    <div style="margin: 5px;">
        <a
            :href="url"
            @click="autoOpenAlink"
            target="_blank"
            >
            {{url}}
        </a>
    </div>

vue

    autoOpenAlink(e) {
        e.preventDefault();
        let url = this.url;
        window.open(url, "iframe testing page");
    },

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


   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> 

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

You can do

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

and it worked successfully on my website.


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.


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


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.


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


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


Open another url like a click in link

window.location.href = "http://example.com";

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);
}

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

window.open('?','_self');

Use this:

location.href = "http://example.com";

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment Wrapping a react-router Link in an html button How to make a hyperlink in telegram without using bots? React onClick and preventDefault() link refresh/redirect? How to put a link on a button with bootstrap? How link to any local file with markdown syntax? link with target="_blank" does not open in new tab in Chrome How to create a link to another PHP page How to determine the current language of a wordpress page when using polylang? How to change link color (Bootstrap) How can I make a clickable link in an NSAttributedString?

Examples related to href

File URL "Not allowed to load local resource" in the Internet Browser Angular 2 router no base href set How to redirect to a route in laravel 5 by using href tag if I'm not using blade or any template? Html- how to disable <a href>? Add php variable inside echo statement as href link address? ASP MVC href to a controller/view href="tel:" and mobile numbers How can I disable the bootstrap hover color for links? How can I disable HREF if onclick is executed? Open link in new tab or window