我正在做一个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) + ")"});
});