页面链接跳转历史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/

原文地址:https://www.cnblogs.com/alantao/p/7298906.html