简体   繁体   English

在phonegap中的2个html页面之间导航

[英]Navigating between 2 html pages in phonegap

I am working on phonegap application. 我正在研究phonegap应用程序。 I am facing one issue. 我正面临一个问题。

I have four html pages in my application. 我的应用程序中有四个html页面。 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. 也就是说,当我尝试使用设备的后退按钮返回时,它会关闭该应用程序。

I am using device's back button and not user defined, so i need to handle that. 我正在使用设备的后退按钮,而不是用户定义的,因此我需要进行处理。 same as onBackPressed(); onBackPressed();相同onBackPressed(); in android. 在android中。

I know it is because of the WebView widget. 我知道是因为有WebView小部件。 but unable to find solution. 但找不到解决方案。

I am new to JavaScript, CSS, AJAX, jQuery and HTML5. 我是JavaScript,CSS,AJAX,jQuery和HTML5的新手。

How to handle back press in phonegap? 如何处理phonegap中的后按?

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. 看看phonegap文档。 http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html#backbutton http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html#backbutton

Let us know whether it helps you! 让我们知道它是否对您有帮助!

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后退按钮

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(); }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM