location

    location 的数据类型是对象,它拆分并保存了 URL 地址的各个组成部分

    常用属性:

    属性 描述
    ancestorOrigins
    href 获取完整的URL地址,对其赋值用于地址的跳转
    origin
    protocol
    host
    hostname
    port
    pathname
    search 获取地址中携带的参数,符号 ? 后面部分
    hash 获取地址中的哈希值,符号 # 后面部分

    常用方法:

    方法 描述
    reload() 刷新当前页面,传入参数 true 时表示强制刷新
    1<!DOCTYPE html>
    2<html lang="en">
    3  <head>
    4    <meta charset="UTF-8" />
    5    <title>location</title>
    6  </head>
    7  <body>
    8    <a href="http://www.codebetter.cn">支付成功,5秒之后跳转到首页</a>
    9    <script>
    10      // 1.获取a元素
    11      const a = document.querySelector("a");
    12      // 2.开启定时器
    13      // 3.声明倒计时变量
    14      let num = 5;
    15      let timer = setInterval(() => {
    16        num--;
    17        a.innerHTML = `支付成功,${num}秒之后跳转到首页`;
    18        if (num === 0) {
    19          clearInterval(timer);
    20          location.href = "http://www.codebetter.cn";
    21        }
    22      }, 1000);
    23    </script>
    24  </body>
    25</html>