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


当前回答

在我开始之前, jQuery 是用于 DOM 操作的 JavaScript 库。 所以您不应该使用 jQuery 重定向页面 。

Jquery.com的一段引文:

虽然jQuery在旧浏览器版本中可能运行没有大问题,但我们并不积极测试jQuery在其中的位置,而且通常不修补其中可能出现的错误。

发现于此:https://jquery.com/browser-support/

所以jQuery并不是一个终极和万能的解决方案 解决逆向兼容性。

以下使用原始 JavaScript 的解决方案在所有浏览器中都起作用, 并且已经相当长的时间达到了标准, 所以您不需要任何库来支持交叉浏览器 。

此页面将重定向到谷歌谷歌3000毫秒后

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

使用替换时, 后端按钮将不再返回重置页面, 仿佛它从来没有在历史中出现过。 如果您想要用户能够返回重置页面, 请使用window.location.hrefwindow.location.assign。如果您确实使用让用户返回重定向页面的选项,请记住,当您输入重定向页面时,它会将您重新定向。因此,在选择重定向选项时会考虑到这一点。如果该页面仅在用户采取行动时才被重定向,那么在后按钮历史中拥有页面就没事了。但如果页面自动重定向,那么您应该使用替换,这样用户就可以使用后端按钮,而不必被迫返回重定向发送的页面。

您也可以使用元数据运行页面重定向 。

META 刷新

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

METTA 地点

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

BASE 劫持

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

有许多方法可以将您未预料到的客户端转到他们可能不希望访问的页面上(其中没有一个是依赖jQuery):

https://code.google.com/p/html5security/wiki/RedirectionMethods

我还要指出,人们不喜欢随机调整方向。 只有在绝对需要的时候才会调整方向。 如果您开始随机调整方向, 他们就不会再去你的网站了 。

下一段是假设性的:

您也可以被报告为恶意网站。如果发生这种情况,当人们点击与您网站的链接时,用户浏览器可能会警告他们您的网站是恶意的。同样可能发生的是搜索引擎可能会开始降低您的评级,如果有人在您的网站上报告有不良经历的话。

请审查Google网站技术主管指南关于改方向的内容:https://support.google.com/webmasters/answer/2721217?hl=en&ref_topic=6001971

这是一个有趣的小页 踢你出页面。

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

如果您将两个页面示例合并在一起, 你会有一个新生的路线改变循环, 这将保证您的用户将永远不再想要使用您的网站 。

其他回答

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

$(location).prop('href', 'http://stackoverflow.com')

我们不能简单地使用 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.href = 'your_url';
  1. 指定位置( ) :

    将一条路径通过一条路径进入它 来指定一条路径...指派即使在指定路径之后, 也会给你们一个历史 。

    用法:数值应传递到它中 。

    例如: location.assign("http://google.com")

    Enter image description here

  2. 位置.href

    定义可以给它一个路径... 并且一旦它建立,它会重新定位到一个指定路径, 它会保存历史...

    用法:值应该被指定到它中 。

    例如: location.href = "http://google.com"

  3. 位置. replace () :

    如果您不想保留历史, 它会帮助您替换一条路径。 一旦您替换了一条路径, 它不会给您一个历史 。

    用法:数值应传递到它中。

    例如: location.replace("http://google.com")

    Enter image description here


assign()href两者相似,可以保留历史。assign通过分配,通过传递一个价值和粗略的作品来工作。

您可以使用 JavaScript 自己实现它, 而不用 jQuery 来指定 :

window.location = "http://google.com"
location.href = "http://google.com"

使用下面的jQuery 也可以实现类似的目标。 它会做与上面完全相同的,

$(window).attr('location', "http://www.google.com");
$(location).attr('href', "http://www.google.com");

你可以很容易地理解 两者之间的区别...

这里是定位对象,

Location API from chrome

使用 :

function redirect(a) {
    location = a
}

并称之为:redirect([url]);

没必要href之后location,因为它是隐含的。