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


当前回答

但是,如果有人想重新引导到主页,那么他可以使用下面的剪辑。

window.location = window.location.host

如果你有三种不同的环境,如发展、排行和生产,这将是有用的。

您可以通过在 Chrome 控制台或 Firebug 的控制台中放置这些单词来探索此窗口或 window.location 对象。

其他回答

JavaScript 是非常广泛的. 如果你想跳到另一个页面,你有三个选项。

 window.location.href='otherpage.com';
 window.location.assign('otherpage.com');
 //and...

 window.location.replace('otherpage.com');

当你想移动到另一个页面,你可以使用其中一个,如果这是你的要求. 但是,所有三个选项都限于不同的情况。

如果你有兴趣了解更多关于这个概念,你可以通过更多。

window.location.href; // Returns the href (URL) of the current page
window.location.hostname; // Returns the domain name of the web host
window.location.pathname; // Returns the path and filename of the current page
window.location.protocol; // Returns the web protocol used (http: or https:)
window.location.assign; // Loads a new document
window.location.replace; // RReplace the current location with new one.

警告:这个答案只是作为一个可能的解决方案提供;它显然不是最好的解决方案,因为它需要jQuery。

$(location).prop('href', 'http://stackoverflow.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";

此分類上一篇: 你可以將延遲時間設定到你想要的任何地方:

<!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.href='blaah.com';
window.location.assign('blaah.com');

和...

window.location.replace('blaah.com');

最后一个是最好的,对于一个传统的重定向,因为它不会保存你去的页面,然后在你的搜索历史中重定向。

EDIT:窗口预定是可选的。