javascript - 在phonegap和inappbrowser之间共享数据

标签 javascript cordova window.open inappbrowser

我正在做一个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。

最佳答案

试试这个:

在您的 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) + ")"});
});

关于javascript - 在phonegap和inappbrowser之间共享数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37280791/

相关文章:

javascript - ng-repeat 和指令的 Angular 性能问题

javascript - 无效的主机 header

Javascript模块模式和函数调用

javascript - 使用 PhoneGap,navigator.contacts.create() 'create' 未定义?

javascript - 如何导出全局工作的模块并用于 ES6 导入

android - ionic +快车道 |安卓 "error: package android.support.v4.content does not exist"

iphone - 手机间隙 : get the filesize of captured audio file

javascript - window.open 作为选项卡在 Chrome 中不弹出

javascript - FireFox 工具栏 在新标签页中打开窗口

javascript - 带有 2 个提交按钮的表单