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


当前回答

如果您想在同一应用程序中重新引导到路线

window.location.pathname = '/examplepath'

这将是走路的方式。

其他回答

您可以在 jQuery 中以以下方式重定向:

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

我已经使用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>

我只是用另一个更新的jQuery方法更新这个荒谬:

var url = 'http://www.fiftywaystoleaveyourlocation.com';
$(location).prop('href', url);

我只是添加另一种方式:

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

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

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