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


当前回答

var url = 'asdf.html';
window.location.href = url;

其他回答

但如果有人想重回主页 他可能会使用下面的片段

window.location = window.location.host

如果你有三种不同的环境,如发展、中转和生产,将会很有帮助。

您可以将这些单词放入铬控制台, 来探索此窗口或窗口。 位置对象 。 @ info: whatsthis火虫控制台

单页应用在同一申请路线内

window.location.pathname = '/stack';

JavaScript :

location.href = "http://stack.com";
window.location = "http://stack.com";

jj 查询 :

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

角形 4

import { Router } from '@angular/router';
export class NavtabComponent{
    constructor(private router: Router) {
    }
    this.router.navigate(['bookings/taxi']);
}

我只是补充另一种方式:

要将网站的任何特定页面/链接重定向到另一页,请添加这一行代码:

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

通过使用这个简单的代码, 您可以重定向整个网站或任何单页 。

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

var currentLocation = window.location;

URL 基本结构

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

enter image description here

  1. 协议 -- 指定协议名称用于访问互联网上的资源。 (HTTP(没有 SSL)或HTPS(没有 SSL))

  2. 主机名 -- -- 主机名指定拥有资源的主机名。例如,www.stackoverflow.com。一个服务器使用主机名提供服务。

  3. 端口 -- -- 用于识别因特网或其他网络电文到达服务器时要发送到的具体过程的端口号。

  4. 路径 - 路径为网络客户端想要访问的主机内的具体资源提供信息。 例如, stackoverflow. com/ index.html 。

  5. 查询 -- -- 查询字符串跟随路径组件,并提供一个资源可用于某种目的的信息字符串(例如,作为搜索的参数或作为要处理的数据)。

  6. hash - URL 的锁定部分, 包括 hash 符号 (#) 。

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

  1. 散数- 发送或返回 URL 的锁定部分。
  2. 主机主机- 发送或返回 URL 主机名和端口。
  3. 主机名- 发送或返回 URL 主机名。
  4. 小时( href)- 设置或返回整个 URL 。
  5. 路径名- set 或返回 URL 的路径名。
  6. 端口端口- set 或返回服务器用于 URL 的端口号。
  7. 议定书议定书- 发送或返回 URL 的协议。
  8. 搜索搜索- set 或返回 URL 的查询部分

如果您现在想要更改页面或将用户重定向到其他页面, 您可以使用该页面href像这样的位置对象的属性

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

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

位置对象还有这三种方法

  1. (指定)- 装入新文档。
  2. 重新装入()- 重新装入当前文档。
  3. (替换)-- -- 将当前文档替换为新文档

您可以使用指派 () 并替换将重定向到其它页面的方法

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

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

转让 () 和替换 () 如何不同- 替换() 方法与指定() 方法() 之间的差别是,替换() 从文档历史中删除当前文档的 URL, 意思是无法使用“ 回溯” 按钮返回原始文档。所以,如果您想要装入新文档,则使用指定() 方法, 并想要给此选项返回原始文档 。

您可以使用jj 查询像这样

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

这样你就可以把用户 转到其他的URL上。

从客户的侧面调整方向:

<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript and jQuery example to redirect a page or URL </title>
    </head>
    <body>
        <div id="redirect">
            <h2>Redirecting to another page</h2>
        </div>

        <script src="scripts/jquery-1.6.2.min.js"></script>
        <script>
            // JavaScript code to redirect a URL
            window.location.replace("http://stackoverflow.com");
            // window.location.replace('http://code.shouttoday.com');

            // Another way to redirect page using JavaScript

            // window.location.assign('http://code.shouttoday.com');
            // window.location.href = 'http://code.shouttoday.com';
            // document.location.href = '/relativePath';

            //jQuery code to redirect a page or URL
            $(document).ready(function(){
                //var url = "http://code.shouttoday.com";
                //$(location).attr('href',url);
                // $(window).attr('location',url)
                //$(location).prop('href', url)
            });
        </script>
    </body>
</html>