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


当前回答

你可以使用:

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

替代() 方法不会在会议历史中保存起源页面,因此用户无法使用后按钮返回并重新转向。

但是,如果你想要一个与点击链接相同的效果,你应该去:

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

在此情况下,浏览器返回按钮将是活跃的。

其他回答

JavaScript 为您提供了许多方法,以获取和更改浏览器的地址栏中显示的当前 URL. 所有这些方法都使用位置对象,这是窗口对象的属性. 您可以创建一个新的位置对象,具有当前 URL。

var currentLocation = window.location;

一个URL的基本结构

<protocol>//<hostname>:<port>/<pathname><search><hash>

此分類上一篇

协议 - 指定协议名称用于访问互联网上的资源. (HTTP(没有SSL)或HTTPS(有SSL))主机名称 - 主机名称指定主机拥有资源. 例如, www.stackoverflow.com. 服务器提供服务,使用主机名称。

使用这些位置对象属性,您可以访问所有这些 URL 组件

现在,如果您想要更改一个页面或将用户转向另一个页面,您可以使用位置对象的href属性,如此。

您可以使用位置对象的 href 属性。

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

位置对象也有这些三种方法

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

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

$(location).attr('href',url);

有三种主要方法可以做到这一点,

window.location.href='blaah.com';
window.location.assign('blaah.com');

和...

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

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

EDIT:窗口预定是可选的。

我已经使用JavaScript的重定向功能(),它工作了。

<script type="text/javascript">
    $(function () {
        //It's similar to HTTP redirect
        window.location.replace("http://www.Technomark.in");

        //It's similar to clicking on a link
        window.location.href = "Http://www.Technomark.in";
    })
</script>

我只是添加另一种方式:

要将您的网站的任何特定页面/链接转向另一个页面,只需添加此代码行:

<script>
    if(window.location.href == 'old_url')
    {
        window.location.href="new_url";
    }

    // Another URL redirect
    if(window.location.href == 'old_url2')
    {
        window.location.href="new_url2";
    }
</script>

作为现实生活的例子,

<script>
    if(window.location.href == 'https://old-site.com')
    {
        window.location.href="https://new-site.com";
    }

    // Another URL redirect
    if(window.location.href == 'https://old-site.com/simple-post.html')
    {
        window.location.href="https://new-site.com/simple-post.html";
    }
</script>

使用这个简单的代码,您可以重新引导完整的网站或任何单页。

此分類上一篇: 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>

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