我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?


当前回答

在 PHP、HTML 或 jQuery 部分后写下下面的代码,如果在 PHP 或 HTML 部分中间,则使用 <script> 标签。

location.href = "http://google.com"

其他回答

它适用于每个浏览器:

window.location.href = 'your_url';

使用 location.replace() 将重定向您,但没有保存前一页的历史。

但是,如果你想保持你的历史,你必须使用 location.href=//path。

例子:

// Form with steps
document.getElementById('#next').onclick = function() {
   window.location.href='/step2' // Iteration of steps;
}

// Go to next step
document.getElementById('#back').onclick = function() {
   window.history.back();
}

// Finish
document.getElementById('#finish').onclick = function() {
   window.location.href = '/success';
}

// On success page
window.onload = function() {
    setTimeout(function() {
       window.location.replace('/home'); // I can't go back to success page by pressing the back button
    },3000);
}
var url = 'asdf.html';
window.location.href = url;

试试这:

location.assign("http://www.google.com");

编码的例子。

一个不只是使用jQuery重定向

jQuery 不需要, window.location.replace(...) 最好是模拟一个 HTTP 重定向。

window.location.replace(...) 比使用 window.location.href 更好,因为 replace() 不保留起源页面在会话历史中,这意味着用户不会陷入无止境后按钮故障。

如果你想模拟某人点击链接,使用 location.href

如果您想模拟一个 HTTP 重定向,请使用 location.replace。

例如:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";