问题
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