[英]prompt user on backspace and browser backbutton in gwt
從一個班級導航到另一個創建的歷史記錄時,我有兩個班級。 現在,我想通過確認用戶是否要離開頁面來提示用戶。直到現在,我嘗試在gwt
使用Window.closingHandler()
,但不適用於退格按鈕和瀏覽器后退按鈕,僅在關閉時有效整個瀏覽器或使用交叉的特定頁面。 它也在重新加載。
我也嘗試過Javascript,使用onbeforeunload()
可以完美地工作。
這是我使用的代碼。
JAVASCRIPT:
window.onbeforeunload = function () {
return "Are you sure you wish to leave this delightful page?";
}
還有gwt中的其他代碼:
Window.addWindowClosingHandler(new Window.ClosingHandler() {
public void onWindowClosing(ClosingEvent event) {
event.setMessage("Do you wanna close?");
System.out.println("Closing...");
}
});
我希望在gwt
完成它。
您是否嘗試過使用History.addValueChangeHandler
偵聽瀏覽器的歷史記錄堆棧中的更改?
-編輯-
下面的代碼對於Firefox
, Chrome
和IE9
Backspace key
正常工作。
注意:請同時對其他瀏覽器進行測試,如果有任何問題,請通知我。
Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(final NativePreviewEvent event) {
boolean isFirefox = checkBrowser("Firefox");
if ((isFirefox && event.getTypeInt() == Event.ONKEYPRESS)
|| (!isFirefox && event.getTypeInt() == Event.ONKEYDOWN)) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_BACKSPACE) {
Element element = Element.as(event.getNativeEvent().getEventTarget());
String tagName = element.getTagName();
System.out.println(tagName);
if (!tagName.equalsIgnoreCase("INPUT")
&& !tagName.equalsIgnoreCase("TEXTAREA")) {
boolean result = Window.confirm("Are you sure?");
if (!result) {
event.cancel();
}
}
}
}
}
});
if (checkBrowser("Firefox")) {
DOM.sinkEvents(RootPanel.get().getElement(), Event.ONKEYPRESS);
} else {
DOM.sinkEvents(RootPanel.get().getElement(), Event.ONKEYDOWN);
}
....
private static boolean checkBrowser(String browserName) {
return (Window.Navigator.getUserAgent().toLowerCase().indexOf(browserName.toLowerCase()) != -1);
}
-編輯-
這也是代碼檢測瀏覽器的“后退”按鈕。 欲了解更多信息,請看
public native void onBeforeUnload()/*-{
$wnd.onbeforeunload = function(e) {
return 'Are you sure?';
};
}-*/;
public void onModuleLoad() {
onBeforeUnload();
}
屏幕截圖(單擊瀏覽器后退按鈕或刷新頁面)
請看以下帖子:
我已經在JSNI中找到了它,但是在瀏覽器后退按鈕中也無法正常工作。
public native void call()/*-{
$wnd.onkeypress = GetChar;
function GetChar (event)
{
var key = event.keyCode;
var bb = event.target.nodeName;
if(key==8 && bb=="BODY")
{
var x= window.confirm("Are you sureyou want to leave the page");
if (x==true)
{
window.history.back();
}
else if(x==false)
{
return false;
}
}
}
}-*/;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.