0

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?

0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Would I still need the brackets around the quotes?
@StigCoder - I don't understand what you're asking. You assign a string URL to window.location.
@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.
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();

Comments

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

1 Comment

Although 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.