简体   繁体   中英

Opening another window in Javascript when user presses X on browser

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

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.

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");}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM