首页 文章

在phonegap和inappbrowser之间共享数据

提问于
浏览
1

我正在做一个phonegap应用程序,我无法在index.html和inappbrowser窗口之间共享数据 . 我尝试这个代码,但它不适合我 .

function addUsuario(){
    var fnac = document.getElementById("fnac").value;
    var direccion = (document.getElementById("direccion").value).replace(/\s/g,'%20');
    var descripcion = (document.getElementById("descripcion").value).replace(/\s/g,'%20');
    //localStorage.setItem('"+direccion+"', direccion);


    var ref = window.open('./geo.html', '_blank');
    ref.addEventListener( "loadstart", function() {
    var dire=document.getElementById("direccion").value;
    ref.executeScript({ 
        code: "alert("+dire+");" 
    });
});

    setTimeout(function () {

        var lat = localStorage.getItem('lat');
        var lng = localStorage.getItem('lng');
        alert(lng);

        $('#datos').load("http://192.168.1.173/PHP_BS/mod_usuario.php?usuario=" + user + "&token=" + token+"&fnac="+fnac+"&dire="+direccion+"&descripcion="+descripcion+"&latitud="+lat+"&longitud="+lng);

        ref.close();
    }, 11500);


    }

我也尝试使用localStorage,但只将inappbrowser中的数据共享到index.js,但不是从index.js到inappbrowser .

1 回答

  • 0

    试试这个:

    在您的geo.html页面中:

    <script>
      function onReceiveData (serialisedData) {
         var data = JSON.parse(serialisedData);
         alert('received some data!');
         console.log(data);
         // Do something with the data
      }
    </script>
    

    在您的Cordova代码中:

    var ref = window.open('./geo.html', '_blank');
    ref.addEventListener('loadstop', function() {
        var params = {foo: 1};
        // Since executeScript works with a string, we need to serialise the data we send.
        ref.executeScript({code: "onReceiveData(" + JSON.stringify(params) + ")"});
    });
    

相关问题