程序员的知识教程库

网站首页 > 教程分享 正文

js获取浏览器返回按钮事件(js获取浏览器地址栏参数)

henian88 2024-10-28 15:47:02 教程分享 4 ℃ 0 评论

前面要加上jquery

比如<script src="style/jquery.min.js"></script>

监听事件

jQuery(document).ready(function($) {
 
  if (window.history && window.history.pushState) {
 
    $(window).on('popstate', function() {
      var hashLocation = location.hash;
      var hashSplit = hashLocation.split("#!/");
      var hashName = hashSplit[1];
 
      if (hashName !== '') {
        var hash = window.location.hash;
        if (hash === '') {
          alert('後退按鈕點擊');
        }
      }
    });
 
    window.history.pushState('forward', null, './#forward');
  }
 
});

History 对象

History 对象包含用户(在浏览器窗口中)访问过的 URL。

History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问。

history.pushState(state, title, url)

将当前URL和history.state加入到history中,并用新的state和URL替换当前。不会造成页面刷新。

state:与要跳转到的URL对应的状态信息。

title:不知道干啥用,传空字符串就行了。

url:要跳转到的URL地址,不能跨域。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表