鱼C论坛

 找回密码
 立即注册
查看: 1862|回复: 2

[庖丁解牛] 0 1 0 1 ★ history对象 属性 & 方法大全

[复制链接]
发表于 2017-4-21 10:53:25 | 显示全部楼层 |阅读模式
购买主题 已有 1 人购买  本主题需向作者支付 3 鱼币 才能浏览

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-12 09:50:09 | 显示全部楼层
本帖最后由 a211827754 于 2018-9-12 09:52 编辑

history.forward()和history.go()可以演示的,用loaction.assign("https://www.baidu.com")或者location.replace("http://www.fishc.com")产生历史记录,然后再退回来就可以操作了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-12-27 16:26:40 | 显示全部楼层
学习了
按我的理解写了一个 模拟历史列表的例子 希望能帮到世界上下一个看到这的人吧
<!DOCTYPE html>
<html>

<head>
  <title>History Example</title>
</head>

<body>
  <button onclick="history.back()">返回</button>
  <button onclick="history.forward()">前进</button>
  <button onclick="history.go(-1)">使用Go(-1)方法返回</button>
  <button onclick="addHistory()">模拟访问一个网址</button>
  <button onclick="replaceHistory()">替换列表中当前记录</button>

  <p>当前页面:<span id="current-page">Home</span></p>
  <p>历史记录长度:<span id="history-length">0</span></p>

  <script>
    // 更新页面显示的当前 URL 和历史记录长度
    function updateDisplay() {
      document.getElementById("current-page").innerText = window.location.href;
      document.getElementById("history-length").innerText = history.length;
    }

    // 模拟添加新的历史记录 小巧思之36进制包含数字与英文的随机字符串
    function addHistory() {
      history.pushState({}, "New Page", "?page=" + Math.random().toString(36).substring(7));
      updateDisplay(); // 更新显示
    }

    // 替换当前的历史记录
    function replaceHistory() {
      history.replaceState({}, "Replaced Page", "?replaced=" + Math.random().toString(36).substring(7));
      updateDisplay(); // 更新显示
    }

    // 监听浏览器的 "popstate" 事件,用于处理返回和前进操作
    window.addEventListener("popstate", function () {
      updateDisplay(); // 当用户返回或前进时,更新显示内容
    });

    // 初始化页面显示
    updateDisplay();
  </script>
</body>

</html>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-3 02:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表