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


当前回答

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.

其他回答

您可以使用以下方法重定向页面:

  1. 通过在头部使用元标记 -<meta http-equiv="refresh" content="0;url=http://your-page-url.com" />注意:content="0;... 用于在您需要多少秒后重定向页面

  2. 使用 JavaScript :window.location.href = "http://your-page-url.com";

  3. 使用 jQuery 查询 :$(location).attr('href', 'http://yourPage.com/');

所以,问题在于如何改变页面方向, 而不是如何改变网页方向?

您只需要为此使用 JavaScript 即可。 这里有一些小代码, 可以创建一个动态重定向页面 。

<script>
    var url = window.location.search.split('url=')[1]; // Get the URL after ?url=
    if( url ) window.location.replace(url);
</script>

所以说,你只是把这个片段redirect/index.html在您网站上的文档中,您可以这样使用它 。

http://www.mywebsite.com/redirect?url=http://stackoverflow.com

如果你去那个链接 它会自动引导你到堆叠流. com.

链接到文档

这就是你如何制造简单使用 JavaScript 重定向页面

编辑 :

还有一件事需要指出。window.location.replace在我的代码中,因为我认为它适合 重置页面, 但是,你必须知道,当使用window.location.replace当您在浏览器中按回按钮时,它会被重定向回到重定向页面, 它会回到之前的页面, 看看这个小演示的东西。

示例:

进程:家居存储店 => 将页面重定向到谷歌 => 谷格

当谷歌:谷格 => 在浏览器中的后退按钮 => 家居存储店

因此,如果这适合您的需求, 那么一切都应该很好。 如果您想要在浏览器历史中包含重置页面, 替换此

if( url ) window.location.replace(url);

if( url ) window.location.href = url;

在我开始之前, 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>

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

尝试此 :

location.assign("http://www.google.com");

示例代码片断.

在 PHP 、 HTML 或 jQuery 区域之后写入下面的代码。如果在 PHP 或 HTML 区域中间,则使用 < shit> 标记。

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