You could use the global location.replace
function:
$("#button1").click(function() {
location.replace("/page2.html");
});
Though this is functionally equivalent to using an HTML <a>
tag with an href attribute of "/page2.html", and not using javascript at all, which would be the simpler and therefore better way of doing it. If you want the page to load in the background and only to change the DOM when it has loaded, I suppose you could use an AJAX request:
var ajaxRunning = false;
$("#button1").click(function() {
if (!ajaxRunning) { // don't send two requests at once
ajaxRunning = true;
$.ajax("/page2.html", {
method: "GET",
dataType: "html"
}).done(function(html) {
ajaxRunning = false;
document.write(html);
});
}
});
There's absolutely no real benefit to doing that though (and the cost of greater complexity), unless "page2.html" is a partial HTML file that you're going to use only to update part of the DOM (with the jQuery html
or text
functions, instead of document.write
).
So unless you have a compelling reason to introduce javascript, just use an HTML <a>
tag, like so:
<a href="/page2.html">
<button id="button1">Click me!</button>
</a>