我正在做一个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。
答案 0 :(得分: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) + ")"});
});