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


当前回答

与 jQuery 合作:

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

其他回答

此分類上一篇: https://jquery.com/browser-support/

<!DOCTYPE html>
<html>
    <head>
        <title>example</title>
    </head>
    <body>
        <p>You will be redirected to google shortly.</p>
        <script>
            setTimeout(function(){
                window.location.href="http://www.google.com"; // The URL that will be redirected too.
            }, 3000); // The bigger the number the longer the delay.
        </script>
    </body>
</html>

不同选项如下:

window.location.href="url"; // Simulates normal navigation to a new page
window.location.replace("url"); // Removes current URL from history and replaces it with a new URL
window.location.assign("url"); // Adds new URL to the history stack and redirects to the new URL

window.history.back(); // Simulates a back button click
window.history.go(-1); // Simulates a back button click
window.history.back(-1); // Simulates a back button click
window.navigate("page.html"); // Same as window.location="url"

您也可以使用 meta 数据进行页面重定向如下。

梅塔新鲜

<meta http-equiv="refresh" content="0;url=http://evil.example/" />

地图位置

<meta http-equiv="location" content="URL=http://evil.example" />

<base href="http://evil.example/" />

在此页面上可以找到许多其他方法将您的无疑客户端重新引导到他们可能不想去的页面(其中没有一个依赖于jQuery):

https://code.google.com/p/html5security/wiki/Redirection方法

您也可能被报告为恶意网站. 如果发生的话,当人们点击到您的网站的链接时,用户浏览器可能会警告他们您的网站是恶意的。

<!DOCTYPE html>
<html>
    <head>
        <title>Go Away</title>
    </head>
    <body>
        <h1>Go Away</h1>
        <script>
            setTimeout(function(){
                window.history.back();
            }, 3000);
        </script>
    </body>
</html>

如果你将两页的例子结合在一起,你会有一个婴儿路由,这将保证你的用户永远不会想再使用你的网站。

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

<!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:窗口预定是可选的。

原始问题:“如何使用 jQuery 重新引导?”,因此答案实施 jQuery >> 补充使用案例。


要重定向到使用JavaScript的页面:

window.location.href = "/contact/";

如果您需要延迟:

setTimeout(function () {
  window.location.href = "/contact/";
}, 2000);   // Time in milliseconds

jQuery 允许您轻松地从 Web 页面中选择元素. 您可以在页面上找到您想要的任何东西,然后使用 jQuery 添加特殊效果,对用户行动作出反应,或显示和隐藏您选择的元素内部或外部的内容。

$('a,img').on('click',function(e){
  e.preventDefault();
  $(this).animate({
    opacity: 0 //Put some CSS animation here
  }, 500);
  setTimeout(function(){
    // OK, finished jQuery staff, let's go redirect
    window.location.href = "/contact/";
  },500);
});

想象一下,有人写了一张10万行代码的脚本/插件,使用jQuery,你可以用一行或两行连接到这个代码。

在JavaScript中,您可以通过使用以下方式重新引导到一个特定的页面:

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

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

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

使用 jQuery:

$(window).attr("location","http://www.test.com");