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


当前回答

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

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

其他回答

根据我的工作经验 JavaScript 更适合重新定位

它取决于您要如何更改地址。 如果您想要在用户历史中登录您的网站, 请使用window.location.href='ur website';否则在不记录历史的情况下使用window.location.replace("your website");.

警告: 警告:答案只是作为一种可能的解决办法而已;显然最好的解决方案,因为它需要jQuery。 相反,它更喜欢纯 JavaScript 解决方案。

$(location).prop('href', 'http://stackoverflow.com')
var url = 'asdf.html';
window.location.href = url;

jj 查询,使用$(location).attr('href', url):

$(document).ready(function(){
    var url = "https://www.youtube.com/watch?v=JwMKRevYa_M";
    $(location).attr('href', url); // Using this
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

原始 JavaScript 贾斯克里普特实现这一点有若干途径:

window.location.href="https://www.youtube.com/watch?v=JwMKRevYa_M";

- 明确设定 href 属性。

window.location = "http://www.GameOfThrones.com";

- 是否暗含它 因为窗口。 位置返回一个对象, 默认设置它的. href 属性 。

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

- 将当前窗口的位置替换为新窗口。

self.location = "http://www.somewebsite.com";

- 设置当前窗口本身的位置。

以下是JavaScript在一定时间(3秒)后重新定向的例子:

<script>
    setTimeout(function() {
        window.location.href = "https://www.youtube.com/";
    }, 3000);
</script>

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

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