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);
            });
     
  • 相关阅读:
    四轴PID思路整理
    STM32输入捕获TIM2四通道
    四轴和遥控器的对应
    四层板学习(二)
    四层板学习(一)布线前的准备
    冲突的处理方法
    散列表
    重设置电脑时间
    深圳销量统计源码分析
    源码分析
  • 原文地址:https://www.cnblogs.com/wong-do/p/9238700.html
Copyright © 2011-2022 走看看