.beforeunload只处理刷新
我试图在用户转到应用内的其他页面之前提醒用户,如果有未保存的信息。我遵循several stackoverflows的建议,使用.beforeunload
,但它只在刷新页面时才起作用,而不是在点击指向站点上另一页的链接时。我误解如何使用beforeunload或我需要另一个事件监听器?.beforeunload只处理刷新
的application.js
//= require forms.js
forms.js
$(window).on('beforeunload', function() {
console.log('unloading')
});
的beforeunload
事件是这样的:
// Fires just before leaving/refreshing the page. Handler will
// be passed the event object and if a returnValue is set on the
// event, the browser will display a confirmation dialog with the
// returnValue message displayed.
window.addEventListener("beforeunload", function (evt) {
evt.returnValue = "Setting this to anything but an empty string, causes this to work!";
}, false);
为例见this Fiddle。
那么OPs代码有什么问题? – epascarello
@epascarello她没有在事件对象上设置'returnValue'。正如我的评论所表明的。 –
它应该正常工作的方式OP,它可以在没有returnValue的情况下触发内部的东西。 – epascarello
当您点击链接转到其他网站/页面时,它应该会启动。 – epascarello
@epascarello当您转到网站上的其他页面时,它不会。任何想法为什么? – gwalshington
所以你有它,所以你保存日志页面导航时发生?我们在这里处理什么浏览器? – epascarello