背景:
如果點擊submit按鈕,使用post請求提交表單的話,
再刷新頁面,會提示:
確認重新提交表單
您所查找的網頁要使用已輸入的信息。返回此頁可能需要重復已進行的所有操作。是否要繼續操作。
發現:
jQuery Mobile默認使用ajax方式提交表單。
可以設置$.mobile.ignoreContentEnabled = true;
并在容器上添加data-ajax="false"
來禁用ajax方式提交。
但是,奇怪的是,jQuery Mobile用post方式提交表單后,瀏覽器不會刷新不會提示“確認重新提交表單”。
原因:
分析jQuery Mobile的源代碼,發現使用了以下方法來禁止提示。
window.history.replaceState( state, state.title || document.title, href );
window.history.replaceState
是html提供的新方法,用來設置瀏覽器歷史狀態。
相關的還有,window.history.pushState
。
總結:
可以使用window.history.replaceState(null,null,window.location.href);
來禁用“確認重新提交表單”。
另外,pushState
和replaceState
可以無刷新的將url改變為同域的其他url。