zoukankan      html  css  js  c++  java
  • 页面链接跳转历史URL不记录的兼容处理

    1.阻止跳转a标签的链接

    2.location.replace(href) 不生成新的历史记录, 但有bug

    3.首先通过HTML5 history.replaceState()方法把当前URL地址替换成以个井号#结尾的目前链接地址;

    4.执行location.replace('')刷新当前地址(此时#会忽略);

    (function(){
        var fnUrlReplace = function (eleLink) {
            if (!eleLink) {
                return;
            }
            var href = eleLink.href;
            if (href && /^#|javasc/.test(href) === false) {
                if (history.replaceState) {
                    // 生成新的URL
                    history.replaceState(null, document.title, href.split('#')[0] + '#');
                    location.replace('');       // 刷新当前页面URL
                } else {
                    location.replace(href);     // 不生成新的历史记录
                }
            }
        };
    
        document.getElementsByTagName('a')[0].onclick = function (event) {
            // 阻止跳转
            if (event && event.preventDefault) {
                event.preventDefault();     
            }
            fnUrlReplace(this);
            return false;
        };
    }());

    参考: http://www.zhangxinxu.com/wordpress/2017/02/page-link-url-history-null-not-record/

  • 相关阅读:
    android个版本对应的SDK level,最新包括android10.0
    SQL语句 存在就更新不存在就插入
    forward和sendredirect
    JavaBean
    Cookie单点登录跨域问题
    JSP
    JSP内置对象
    Spring学习笔记
    事务
    AOP实现方式
  • 原文地址:https://www.cnblogs.com/alantao/p/7298906.html
Copyright © 2011-2022 走看看