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.
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);
}
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 ~~
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)
}
window/tab.https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Syntax
_self<a
href="url"
target="_self">
open
</a>
const autoOpenAlink = (url = ``) => {
window.open(url, "open testing page in the same tab page");
}
_blankvue 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");
},
_selfand_topwhich "look-alike". - My Work