使用 javascript/jquery 检测 android 后退按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47092384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use javascript/jquery detect android back button
提问by monkeyT4A
Currently I want to make a function to detect android back button is pressed and do some stuff on it.
目前我想做一个功能来检测android后退按钮是否被按下并在上面做一些事情。
1) User opens the apps, and clicks a button. This button lets the user to open a website.
1) 用户打开应用程序,然后单击一个按钮。此按钮允许用户打开网站。
2) User fills in the information on page 2 , and he wants to go back the previous page, he clicks the back button on android phone.
2) 用户填写第2页信息,想返回上一页,点击安卓手机的后退按钮。
3) The back button helps user close the website and go back to my apps instead of back to page 1.
3) 后退按钮帮助用户关闭网站并返回我的应用程序而不是返回到第 1 页。
Can someone tell me how to do with jqueryor javascriptto check the back button pressed by user? So that I can prevent the back button close the website but redirect to another page.
有人可以告诉我如何处理jquery或javascript检查用户按下的后退按钮吗?这样我就可以防止后退按钮关闭网站但重定向到另一个页面。
If there is any way to detect browser android back button event? I want to override the default setting of android back button, and add some code inside.
如果有什么方法可以检测浏览器android后退按钮事件?我想覆盖android后退按钮的默认设置,并在里面添加一些代码。
PS : not using Java.
PS:不使用Java。
- The back button on top(browser back button) can goes back to previos page.
- The back button on bottom(back button phone) help me close the website.
- I want make the bottom back button alsso goes back to previous page.
- 顶部的后退按钮(浏览器后退按钮)可以返回上一个页面。
- 底部的后退按钮(后退按钮电话)帮助我关闭网站。
- 我想让底部的后退按钮也回到上一页。
Thanks.
谢谢。
回答by nitinkumarp
You need to override the onBackPressed on WebView Activity and check if webview can go back or not.
您需要覆盖 WebView Activity 上的 onBackPressed 并检查 webview 是否可以返回。
@Overide
public void onBackPressed(){
if(webview.canGoBack())
webview.goBack();
else
super.onBackPressed();
}
UPDATE:
更新:
When this answer was given question was not specific for "Javascript only"and "not coding in Java", etc.. i.e. handle only by backend. This answer is strictly for Android side not for backend side. I am keeping this answer if someone from Android need this answer.
当给出这个答案时,问题不是特定于“仅使用 Javascript”和“不使用 Java 编码”等。即仅由后端处理。此答案仅适用于 Android 端,而不是后端端。如果来自 Android 的人需要这个答案,我会保留这个答案。
回答by FrontEndOnDemand
You can achieve it using javascript also. 1.Use below method or try to find deviceReady method.
您也可以使用 javascript 来实现它。1.使用下面的方法或尝试找到deviceReady方法。
<body onload="onLoad()">
2.Put below code in your main.js. This function will get call once body will get load.
2.将下面的代码放在你的main.js中。一旦身体得到负载,这个函数就会被调用。
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
3.On device ready is function which gets trigger once your device is ready.
3.On device ready 是在您的设备准备好后触发的功能。
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
4.Once your ready, you can override your back button functionality.
4.准备好后,您可以覆盖后退按钮功能。
function onBackKeyDown() {
// Do stuff here
}
May I know is this what you was looking for?
我可以知道这是你要找的吗?
OR
或者
you can use hashChangeevent
你可以使用hashChange事件
window.addEventListener("hashchange", function(e) {
// ...
})
If you need to support older browsers, check out the hashChange Eventsection in Modernizr's HTML5 Cross Browser Polyfills wiki page.
如果您需要支持旧浏览器,请查看Modernizr 的 HTML5 Cross Browser Polyfills wiki 页面中的hashChange Event部分。
回答by Michael Warner
I would say you should focus on the feature and not the device's implementation of that feature.
我会说您应该关注该功能,而不是该功能的设备实现。
The Android Back Button has the same behavior as the browser back button.
Android 后退按钮与浏览器后退按钮的行为相同。
As both react to the URL state you can control the behavior through the URL. You can achieve this through pushState. You can read about pushState from MDN hereand here is a CSS Trickarticle about using it.
由于两者都对 URL 状态做出反应,因此您可以通过 URL 控制行为。您可以通过 pushState 实现这一点。您可以从MDN了解pushState的这里,这里是一个CSS招数有关使用它的文章。
回答by Bashirpour
You can do this with javascriptand without jquery
你可以用javascript而不用jquery来做到这一点
window.addEventListener("hashchange", function(e) {
if(e.oldURL.length > e.newURL.length)
alert("back")
});

