使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?


当前回答

您可以使用:

window.location.replace("http://www.example.com/");

缩略replace()方法不会保存会话历史中的源页, 因此用户无法使用后端按钮返回, 然后再被重定向 。 注意: 在此情况下, 浏览器后端按钮将被禁用 。

但是,如果您想要与点击链接相同的效果,您应该选择:

window.location.href = "http://www.example.com/";

在此情况下, 浏览器后端按钮将激活 。

其他回答

在 JavaScript 和 jQuery 中, 我们可以使用以下代码将一页改到另一页 :

window.location.href="http://google.com";
window.location.replace("page1.html");

在单击功能上,只需添加:

window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
    window.location.href = "http://www.google.com";
});

使用 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;

以下是一个时间延迟重定向。 您可以将延迟时间设定为任何您想要的时间 :

<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Your Document Title</title>
    <script type="text/javascript">
        function delayer(delay) {
            onLoad = setTimeout('window.location.href = "http://www.google.com/"', delay);
        }
    </script>
</head>

<body>
    <script>
        delayer(8000)
    </script>
    <div>You will be redirected in 8 seconds!</div>
</body>

</html>

但如果有人想重回主页 他可能会使用下面的片段

window.location = window.location.host

如果你有三种不同的环境,如发展、中转和生产,将会很有帮助。

您可以将这些单词放入铬控制台, 来探索此窗口或窗口。 位置对象 。 @ info: whatsthis火虫控制台