1

我正在一个使用 jsf 的网站上工作,并从 primefaces 的功能中受益,而我正在管理在卸载前保存更改的想法,我会遇到一个问题。我正在使用 javascript 进行 onbeforeunload,以便在刷新、返回或关闭浏览器之前保存所做的更改,知道使用的是 firefox 浏览器,这是我的 javascript 代码:

Window.onbeforeunload= handleOnClose();
Function handleOnClose(){
If(document.getElementById("MyForm:btnEdit").disabled== true {
If confirm("The page will close, Do you want to save changes?")
Save();
}
}

我正在从支持 bean 调用 javascript 中的 save 方法,使用:

<p:remoteCommand name="Save" actionListener="#{bean.save()}" />

问题是javascript自然触发,但负责保存的方法仅在关闭浏览器时调用,但在返回或刷新时不调用!这个问题有什么想法或提示吗?

4

1 回答 1

0

在 onbeforeunload 事件中创建一个同步的 ajax 请求。

Window.onbeforeunload= handleOnClose();
    Function handleOnClose(){
        If(document.getElementById("MyForm:btnEdit").disabled== true {
            If confirm("The page will close, Do you want to save changes?")
                Save();
                $.ajax({
                    url: '/foo',
                    type: 'GET',
                    async: false,
                    timeout: 4000
                });
            }
        }

这将阻止浏览器最多 4 秒。

于 2013-08-03T05:08:53.080 回答