我希望在用户离开页面然后返回时发生事件。用例是用户在一个页面上,需要离开才能安装一些软件。完成后,它们返回到原始页面。我需要知道用户何时返回,以便页面可以检查安装,然后转发到另一个页面。
您可以使用
load
/unload
或ready
事件和localStorage
来保存案例的状态。
$(window).load(function() {
if(localStorage.getItem("installing") === "true"){
localStorage.setItem("installed", true);
window.location.href = "/redirect-page";
}
});
$(document).ready(function() {
if(localStorage.getItem("installing") === "true"){
localStorage.setItem("installed", true);
window.location.href = "/redirect-page";
}
});
$(window).unload(function() {
localStorage.setItem("installing", true);
});
Javascript版本:
window.load = function(e) {
if(localStorage.getItem("installing") === "true"){
localStorage.setItem("installed", true);
window.location.href = "/redirect-page";
}
};
window.unload =function() {
localStorage.setItem("installing", true);
};