使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
这很容易执行。 您可以使用 :
window.location.href = "http://www.example.com/";
这将记住上一页的历史。 这样可以点击浏览器的后键返回历史 。
或者:
window.location.replace("http://www.example.com/");
此方法不记得上一页的历史。 在此情况下, 后端按钮会被禁用 。
其他回答
这就是我如何使用它。
window.location.replace('yourPage.aspx');
// If you're on root and redirection page is also on the root
window.location.replace(window.location.host + '/subDirectory/yourPage.aspx');
// If you're in sub directory and redirection page is also in some other sub directory.
我只是不得不更新这种荒谬的 另一种新的jQuery方法:
var url = 'http://www.fiftywaystoleaveyourlocation.com';
$(location).prop('href', url);
使用 jQuery/JavaScript 重定向用户
使用 jQuery 或 JavaScript 中的定位对象, 我们可以将用户重定向到另一个网页 。
在 jQuery 中
将用户从一页转到另一页的代码是:
var url = 'http://www.example.com';
$(location).attr('href', url);
在 JavaScript 中
将用户从一页转到另一页的代码是:
var url = 'http://www.example.com';
window.location.href = url;
或
var url = 'http://www.example.com';
window.location = url;
您可以在脚本部分的按钮点击事件下写入 Url. 动作 。
function onclick() {
location.href = '@Url.Action("Index", "Home")';
}
var url = 'asdf.html';
window.location.href = url;