• js 推断 当页面无法回退时(history.go(-1)),关闭网页


    在做一个Web项目时遇到一个需求,当页面没有前驱历史记录时(就是当前为新弹出的页面,没法做goback操作即history.go(-1)),点击返回button时直接关闭页面,否则就退回到前一页。

    遇到的问题就是怎样推断 是否有history能够回退,这个很麻烦,由于没有这种函数直接能获取到。仅仅能通过history.length这个变量做变通的处理。可是对于IE。和非IE的length的返回值不同,ie: history.length=0, 非IE的为1。因此写了一个函数实现前面所需求的这个功能。分享给大家。

    /**
     * 返回前一页(或关闭本页面)
     * <li>假设没有前一页历史。则直接关闭当前页面</li>
     */
    function goBack(){
        if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)){ // IE
            if(history.length > 0){
                window.history.go( -1 );
            }else{
                window.opener=null;window.close();
            }
        }else{ //非IE浏览器
            if (navigator.userAgent.indexOf('Firefox') >= 0 ||
                navigator.userAgent.indexOf('Opera') >= 0 ||
                navigator.userAgent.indexOf('Safari') >= 0 ||
                navigator.userAgent.indexOf('Chrome') >= 0 ||
                navigator.userAgent.indexOf('WebKit') >= 0){
    
                if(window.history.length > 1){
                    window.history.go( -1 );
                }else{
                    window.opener=null;window.close();
                }
            }else{ //未知的浏览器
                window.history.go( -1 );
            }
        }
    }


  • 相关阅读:
    Python 编码格式的使用
    解决cmd 运行python socket怎么终止运行
    解决win10子系统Ubuntu新装的mysql 不能root登陆方法
    浏览器中网址访问过程详解
    POJ 2502 最短路
    HDU 2859
    POJ 3186
    POJ 1661 暴力dp
    POJ 1015 陪审团问题
    CodeForces 1058E
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5221485.html
Copyright © 2020-2023  润新知