zoukankan      html  css  js  c++  java
  • 监听浏览器返回按钮并实现 点击返回关闭当前页面的功能

    监听浏览器返回按钮并实现 点击返回关闭当前页面的功能

    关闭当前页面(只在ie中生效)

    window.close()

    window.close()只能关闭弹窗和window.open()打开的窗口

    chrome,ff写法

    window.opener = null
    window.open("about:blank","_self","")
    window.close()

    about:blank打开一个空白页面

    浏览器的返回可以用监听事件window.addEventListener('popstate',function(){})来实现,具体代码如下:

    var tempHash = window.location.hash;
    if(window.history && window.history.pushState){
        window.addEventListener('popstate',function(){
            var hasLocation = location.hash;
            var hashSplit = hashLocation.split("#");
            var hashName = hashSplit[1];
            if(hashName != ''){
                var hash = window.location.hash;
                if(hash == tempHash){
                    window.opener = null
                    window.open("about:blank","_self","")
                    window.close()
                }
            }
        });
        window.history.replaceState('forward',null,window.location.hash + '#forward');
        window.history.pushState('forward',null,tempHash);
        tempHash = window.location.hash;
    }
    window.location.hash 通过识别#forward来确定当前页面,女朋友明天就要来了,好开心,不写了
    另外也可以实现屏蔽浏览器的回退按钮
           history.pushState(null, null, document.URL);
            window.addEventListener('popstate', function () {
                history.pushState(null, null, document.URL);
            });
     
  • 相关阅读:
    Linux 服务器连接远程数据库(Mysql、Pgsql)
    oracle主键自增
    全排列算法实现
    python动态导入包
    python发红包实现
    CentOS 6.8安装Oracle 11 g 解决xhost: unable to open display
    xargs的一个小坑
    利用ssh-copy-id复制公钥到多台服务器
    redhat 5 更换yum源
    【原创】Hadoop的IO模型(数据序列化,文件压缩)
  • 原文地址:https://www.cnblogs.com/wong-do/p/9238700.html
Copyright © 2011-2022 走看看