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);
            });
     
  • 相关阅读:
    燃料
    Widgets学习
    莫比乌斯反演与杜教筛
    jade在命令行实时编译
    jade文档声明和头尾标签
    团队成员之间的对抗与互助的鼓励
    jade简介
    node入口文件分析和目录初始化
    搭建一个项目的前期准备
    判断团队适不适合使用node
  • 原文地址:https://www.cnblogs.com/wong-do/p/9238700.html
Copyright © 2011-2022 走看看