Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 857
user1065055 Avatar asked Dec 10 '11 05:12

user1065055


People also ask

How do I force a link to open in the same tab?

Use _self in target attribute of anchor tag to Open link in same tab in HTML webpage.

What allows you to open another website in the same window?

Press "Ctrl-Shift-T" to open the last tab that you closed. Right-clicking on a link opens the page in a new tab. Alternatively, click on the link by pressing the center button or scroll wheel on your mouse. Most browsers, including Google Chrome, Mozilla Firefox and Apple Safari, also offer tabbed browsing.


13 Answers

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.

like image 178
vdbuilder Avatar answered Oct 02 '22 10:10

vdbuilder


Use this:

location.href = "http://example.com";
like image 37
Dave L. Avatar answered Oct 05 '22 10:10

Dave L.


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

like image 43
andromeda Avatar answered Oct 03 '22 10:10

andromeda


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

window.open('?','_self');
like image 42
Tom Berthold Avatar answered Oct 03 '22 10:10

Tom Berthold


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.

like image 26
Magesh Avatar answered Oct 04 '22 10:10

Magesh


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);
}
like image 22
Muaz Khan Avatar answered Oct 04 '22 10:10

Muaz Khan


Open another url like a click in link

window.location.href = "http://example.com";
like image 29
DarckBlezzer Avatar answered Oct 01 '22 10:10

DarckBlezzer


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

like image 28
Jeffrey Zhao Avatar answered Oct 02 '22 10:10

Jeffrey Zhao


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

like image 37
ijse Avatar answered Oct 02 '22 10:10

ijse


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

like image 39
xgqfrms Avatar answered Oct 01 '22 10:10

xgqfrms


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)
}
like image 25
Adi Prasetyo Avatar answered Oct 01 '22 10:10

Adi Prasetyo


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

like image 38
thanglv Avatar answered Oct 02 '22 10:10

thanglv


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.

like image 34
Samit Avatar answered Oct 04 '22 10:10

Samit