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


当前回答

如果您想要直接重定向到同一应用程序内的路线, 只需

window.location.pathname = '/examplepath'

这将是前进的道路。

其他回答

如果您想要直接重定向到同一应用程序内的路线, 只需

window.location.pathname = '/examplepath'

这将是前进的道路。

与 jQuery 合作 :

$(window).attr("location", "http://google.fr");

我们不能简单地使用 jQuery 重置

j/ 查询没有必要,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";

您可以使用:

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

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

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

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

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

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

<!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>