So I am trying to make a tab system for my website and I have been using window.open("The Website")
except when I use it, the site opens on a new tab. Is there a way to make it open on the same tab?
Asked
Active
Viewed 78 times
0
3 Answers
2
document.location = "your_url"
or
window.location = "your_url";
or you can use jQuery to create link and then click on it
var link $('<a href="your_url" style="display: none;">x</a>');
$('body').append(link);
link.click();

Senad Meškin
- 13,597
- 4
- 37
- 55
2
If you assign to window.location
, then the current window will load a new URL in that tab:
window.location = "http://www.yoursite.com";
See What's the difference between window.location and document.location in JavaScript? for why it is slightly safer (for cross browser compatibility) to use window.location
instead of document.location
, though in most browsers you can use either.
-
Would I still need the brackets around the quotes? – May 15 '15 at 15:02
-
@StigCoder - I don't understand what you're asking. You assign a string URL to `window.location`. – jfriend00 May 15 '15 at 15:03
-
@StigCoder No, you don't need the parentheses around the quotes. `window.open` is a function, so you call it with parentheses - `window.open(url)`. `window.location` is not a function, you assign a string to it with the `=` assignment operator as in the answer. – leftclickben May 15 '15 at 16:03
0
var iframe = $('<iframe src="yoursite.com"></iframe>');
$(document).append(iframe);

AmmarCSE
- 30,079
- 5
- 45
- 53
-
2Although technically, yes, this opens the site "in" (as in "somewhere within") the same tab, this is not the answer to the question. It's also not complete, you would need to at least set dimensions on that `iframe` element. – leftclickben May 15 '15 at 16:08