user4859721
user4859721

Reputation:

How to open a website in the same tab with javascript

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?

Upvotes: 0

Views: 81

Answers (3)

Senad Meškin
Senad Meškin

Reputation: 13756

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

Upvotes: 2

jfriend00
jfriend00

Reputation: 708016

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.

Upvotes: 2

AmmarCSE
AmmarCSE

Reputation: 30607

var iframe = $('<iframe src="yoursite.com"></iframe>');
$(document).append(iframe);

Upvotes: 0

Related Questions