Opening another window in Javascript when user presses X on browser

喜你入骨 提交于 2019-12-11 18:54:51

问题


I understand this question has been asked and either answered or rejected before, but i promise i have a reasonably legit reason for asking. I am doing a Uni course and one of the requirements for the web app we are making is to have a certain page (daily sales report) open once the user presses X on the browser, this is a local file only ans aside from using window.onbeforeunload = window.open("dailyreport.html"); , which opens the page every time I do anything (click links etc) I have hit a brick wall.

Also i forgot to mention we are not allowd to use JSON or jquery at all... sucks but thats what the bosses want

Thanks guys Steve


回答1:


You are looking for the windown.onclose event.

As from here

However, note that this is not supported by all browsers. If it is for a uni project you might be able to get away with it though if your requirements don't specify across-the-board browser compatibility.




回答2:


Try this JSFIDDLE

window.onload = function(){
        var as = document.getElementsByTagName("a");
    var linkClicked = false;
    for(i=0;i<as.length; i++){
        as[i].onclick = function(){

            linkClicked= true;
        }
    }

    window.onbeforeunload = function(){!linkClicked && window.open("dailyreport.html");}
}


来源:https://stackoverflow.com/questions/16635507/opening-another-window-in-javascript-when-user-presses-x-on-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!