javascript 在 phonegap 中的 2 个 html 页面之间导航
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18613861/
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
Navigating between 2 html pages in phonegap
提问by MobiDev
I am working on phonegap application. I am facing one issue.
我正在开发 phonegap 应用程序。我面临一个问题。
I have four html pages in my application. Every page contains some widgets like a button or a list view on click of button i move to next page. but when i want to come back on first page i can't. that is when I try to come back using back button of device it closes the app.
我的应用程序中有四个 html 页面。每个页面都包含一些小部件,例如按钮或单击按钮时的列表视图,我将移至下一页。但是当我想回到第一页时,我不能。那是当我尝试使用设备的后退按钮返回时,它会关闭应用程序。
I am using device's back button and not user defined, so i need to handle that.
same as onBackPressed();
in android.
我使用的是设备的后退按钮而不是用户定义的,所以我需要处理它。和onBackPressed();
安卓一样。
I know it is because of the WebView widget. but unable to find solution.
我知道这是因为 WebView 小部件。但无法找到解决方案。
I am new to JavaScript, CSS, AJAX, jQuery and HTML5.
我是 JavaScript、CSS、AJAX、jQuery 和 HTML5 的新手。
How to handle back press in phonegap?
如何处理phonegap中的背压?
回答by Saxophonist
You can make a workaround to solve this problem.
您可以制定解决方法来解决此问题。
You can define a function to be triggered when back button is pressed and then verify which page your user is in, and depending on each page run a different action. For example, if he is in page3 then you go back to page 2, if page 2 then go back to page 1 and if he is in page1 you can close the application.
您可以定义一个在按下后退按钮时触发的函数,然后验证您的用户所在的页面,并根据每个页面运行不同的操作。例如,如果他在第 3 页,则返回第 2 页,如果第 2 页则返回第 1 页,如果他在第 1 页,则可以关闭应用程序。
Wrote an example for you:
为你写了一个例子:
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
function onBackKeyDown() {
var whichPage = functionToDetectCurrentPage(); //create a function or detect it somehow
switch(whichPage){
case "Page1":
//works only in android, iOS not quite sure, but heard it's not possible
//to do programatically
navigator.app.exitApp();
break;
case "Page2":
window.location = "Page1.html";
break;
case "Page3":
window.location = "Page2.html";
break;
case "Page4":
window.location = "Page2.html";
break;
}
}
Take a look at phonegap documention. http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html#backbutton
看看 phonegap 文档。 http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html#backbutton
Let us know whether it helps you!
让我们知道它是否对您有帮助!
回答by Andrew Grothe
It may not be possible for an inbrowser
app.
inbrowser
应用程序可能无法实现。
See related SO answer at: Handle Android Back Button on Phonegap InAppBrowser
请参阅相关的 SO 答案:处理 Phonegap InAppBrowser 上的 Android 后退按钮
回答by Homen
You can use "backbutton" event for this.
您可以为此使用“后退按钮”事件。
Syntax : document.addEventListener("backbutton", yourCallbackFunction, false);
句法 : document.addEventListener("backbutton", yourCallbackFunction, false);
And you can write your "yourCallbackFunction" like this.
你可以这样写你的“yourCallbackFunction”。
function yourCallbackFunction(){
window.history.back();
}
function yourCallbackFunction(){
window.history.back();
}
Documentation link https://cordova.apache.org/docs/en/4.0.0/cordova/events/events.backbutton.html
文档链接https://cordova.apache.org/docs/en/4.0.0/cordova/events/events.backbutton.html