Javascript 窗口卸载以触发事件
当用户离开页面时,我想让他们确认他们正在离开页面:Javascript 窗口卸载以触发事件,javascript,jquery,Javascript,Jquery,当用户离开页面时,我想让他们确认他们正在离开页面: window.onbeforeunload = function(){ if($('.fancy-blue-button.check-toggle').text() == 'Check In Task'){ return 'Note that this task will automatically be checked back in once you leave this page.'; } }; $(wi
window.onbeforeunload = function(){
if($('.fancy-blue-button.check-toggle').text() == 'Check In Task'){
return 'Note that this task will automatically be checked back in once you leave this page.';
}
};
$(window).unload(function() {
alert('Handler for .unload() called.');
});
第一个,
onbeforeunload
有效,第二个,unload
似乎没有任何作用。可能会将警报更改为console.log,并确保控制台在重定向时未清除!某些浏览器阻止卸载时触发警报,因此只需确保卸载未工作,并且由于浏览器引起的警报不会在卸载功能中发生。您可以尝试使用console.log
<script>
$(window).unload(function () {
console.log("Bye!");
});
</script>
这没用@KhawerZeshan请看更新的问题。请看这里:我建议你看一下之前的帖子:
<script>
$(window).unload(function () {
$.ajax({
type: 'post',
async: false,
url: 'scriptUrlGoesHere'
});
});
</script>