window.history.back()在Phonegap加载外部网站时不起作用

时间:2015-08-10 05:54:08

标签: javascript android cordova jquery-mobile phonegap-plugins

在我的phonegap应用中,我在此事件中添加了deviceready事件,当我打电话给我时,我正在调用另一个网站网址,我正在使用我的进一步申请。但问题是每当我按Back button设备的Android时,应用就会返回我应用的第一页,而不是转到该特定部分的上一页。

我的index.js如下所示:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        document.addEventListener("backbutton", this.onBackKeyDown, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        app.addEventListener("backbutton", this.onBackKeyDown, false);
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var popup = window.open('http://www.example.com/index_mobile', '_blank', 'location=no,toolbar=no,hidden=yes');
        popup.addEventListener("loadstop", function() {
            popup.show();
        });
        popup.addEventListener("backbutton", function (e) {
            alert("Hello popupback");
     e.preventDefault();
        }, false );

        document.addEventListener("backbutton", function(e){
            alert('hello');
            var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;
            alert(app);
            if ( app ) {
                    navigator.app.exitApp();
                }
                else {
                    alert("else");
                    history.go(-1);
                    window.history.back();
                    navigator.app.backHistory()
                }
        }, false);
        console.log('Received Event: ' + id);
    },onBackKeyDown:function(){
        alert("Hello this.onback"); 
    }

};

我的index.html是:

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />

        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, target-densitydpi=medium-dpi, user-scalable=0" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>My App Name</title>
    </head>
    <body>
    <div class="app1">
        <div class="app2">
            <div class="app">
            <div class="app_inner">
                <img src="img/serious_app.png" class="serious_text">
                <p>LOADING...</p>
                </div>
            </div>
        </div>
    </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>
    </body>
</html>

我的config.xml是:

<widget xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" id="com.jg.app" version="1.2.0">
  <name>My App</name>
  <description>Testing</description>
  <author href="http://phonegap.com" email="support@phonegap.com">phonegap</author>
  <content src="index.html"/>
  <preference name="permissions" value="none"/>
  <preference name="orientation" value="default"/>
  <preference name="target-device" value="universal"/>
  <preference name="fullscreen" value="true"/>
  <preference name="webviewbounce" value="true"/>
  <preference name="prerendered-icon" value="true"/>
  <preference name="stay-in-webview" value="false"/>
  <preference name="ios-statusbarstyle" value="black-opaque"/>
  <preference name="detect-data-types" value="true"/>
  <preference name="exit-on-suspend" value="false"/>
  <preference name="disable-cursor" value="false"/>
  <preference name="android-minSdkVersion" value="7"/>
  <preference name="android-installLocation" value="auto"/>
  <gap:plugin name="org.apache.cordova.battery-status"/>
  <gap:plugin name="org.apache.cordova.camera"/>
  <gap:plugin name="org.apache.cordova.media-capture"/>
  <gap:plugin name="org.apache.cordova.console"/>
  <gap:plugin name="org.apache.cordova.device"/>
  <gap:plugin name="org.apache.cordova.device-motion"/>
  <gap:plugin name="org.apache.cordova.device-orientation"/>
  <gap:plugin name="org.apache.cordova.dialogs"/>
  <gap:plugin name="org.apache.cordova.file"/>
  <gap:plugin name="org.apache.cordova.file-transfer"/>
  <gap:plugin name="org.apache.cordova.geolocation"/>
  <gap:plugin name="org.apache.cordova.inappbrowser"/>
  <gap:plugin name="org.apache.cordova.vibration"/>
  <feature name="InAppBrowser">
    <param name="android-package" value="org.apache.cordova.inappbrowser.InAppBrowser" />
</feature>
<feature name="InAppBrowser">
    <param name="ios-package" value="CDVInAppBrowser" />
</feature>
  <icon src="icon.png"/>
  <icon src="www/res/icon/logo_36.png" gap:platform="android" gap:qualifier="ldpi"/>
  <icon src="www/res/icon/logo_48.png" gap:platform="android" gap:qualifier="mdpi"/>
  <icon src="www/res/icon/logo_72.png" gap:platform="android" gap:qualifier="hdpi"/>
  <icon src="www/res/icon/logo_96.png" gap:platform="android" gap:qualifier="xhdpi"/>
  <icon src="www/res/icon/logo_80.png" gap:platform="blackberry"/>
  <icon src="www/res/icon/logo_80.png" gap:platform="blackberry" gap:state="hover"/>
  <icon src="www/res/icon/logo_57.png" gap:platform="ios" width="57" height="57"/>
  <icon src="www/res/icon/logo_72.png" gap:platform="ios" width="72" height="72"/>
  <icon src="www/res/icon/logo_114.png" gap:platform="ios" width="114" height="114"/>
  <icon src="www/res/icon/logo_144.png" gap:platform="ios" width="144" height="144"/>
  <icon src="www/res/icon/logo_64.png" gap:platform="webos"/>
  <icon src="www/res/icon/logo_48.png" gap:platform="winphone"/>
  <icon src="www/res/icon/logo_173.png" gap:platform="winphone" gap:role="background"/>
  <access origin="*"/>
  <plugin name="cordova-plugin-whitelist" version="1"/>
  <allow-intent href="http://*/*"/>
  <allow-intent href="https://*/*"/>
  <allow-intent href="tel:*"/>
  <allow-intent href="sms:*"/>
  <allow-intent href="mailto:*"/>
  <allow-intent href="geo:*"/>
  <feature name="App">
    <param name="android-package" value="org.apache.cordova.App" />
</feature>
  <platform name="android">
    <allow-intent href="market:*"/>
  </platform>
  <platform name="ios">
    <allow-intent href="itms:*"/>
    <allow-intent href="itms-apps:*"/>
  </platform>
</widget>

我已经尝试过各种各样的背压解决方案,但他们并没有帮助我。 我正在从https://build.phonegap.com/apps/构建我的应用 此外,当我按下后退按钮时,它会返回到我的index.html页面;在这个页面上我只是显示加载过程,然后我重定向到我的响应式移动网站。因此,当我重定向到我的网站时,只要按下后退按钮,该页面就不再可见。它就像闪屏一样。

0 个答案:

没有答案