I've seen many examples how to ask user if he really wants to leave a currently opened page, but I've never seen code that would warn user abo开发者_JAVA技巧ut leaving a page only if he's leaving to certain pages, while silently allowing it for other pages.
Basically, I would need a property that gets set to a future page's href when new url is clicked (or some other event triggers).
From a similar question: How can i get the destination url in javascript onbeforeunload event?
What is possible is to capture the onbeforeunload
event. What it is not possible to do is get the url of the page the user is going to. One unorthodox hack is to get the URL of the link last focused on, but that is a infringing on a user's privacy.
You are looking for the onbeforeunload
event.
Example
var dontLeave = function(e){
return "DONT LEAVE MY PRECIOUS!!";
}
window.onbeforeunload=dontLeave;
精彩评论