问题
I'm working on a mobile app on phonegap, and I need to pass variables between pages exactly like : http://coenraets.org/apps/directory/jqm/index.html
but my problem is that I can't use a php file. How can I do that ? Thank you :)
I just tried the LocalStorage which is mentionned here : http://docs.phonegap.com/en/1.6.1/cordova_storage_storage.md.html#localStorage but it doesn't work : here's my code :
Page1 :
$(data).find("Book").each(function () {
var temp = $(this).find("name").text();
var temp1 = $("#champ").val().replace(" ", "") ;
if (temp1 != "") {
if (temp.toLowerCase().search(myRegExp) > -1) {
$("#result_list").append("<li><a href='recherche_details.html' data-transition='pop'><img src='images/a.jpg' /><p><strong>Titre : " + temp + "</strong></p><p>Auteur : " + $(this).find("address").text() + "</p><p>Pays : " + $(this).find("country").text() + "</p></a></li>").listview("refresh") ;
$envoi_search.attr("disabled", "");
// Using the LocalStorage
window.localStorage.setItem("titre" + i, temp);
i++ ;
}
}
});
Page2 :
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
// keyname is now equal to "key"
var value = window.localStorage.getItem("titre0");
$("#result").append("Yoooo" + value + " !! you are here") ;
}
</script>
but it doesn't work. Do you have any idea ?
回答1:
I'd be tempted to use LocalStorage for this, store the data in the first page and retrieve it in the next.
Then clear the localstorage to prevent it from being left behind.
http://docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html#localStorage
回答2:
Set your variable in either the location.hash
or location.search
(querystring), then retrieve it using JavaScript on the second page.
https://developer.mozilla.org/en/DOM/window.location#Properties
Don't forget that when you retrieve location.hash
or location.search
, you'll want to strip the first character (# or ?) using something like .substring(1)
:
var hash = location.hash.substring(1);
Also see this question for techniques to parse a location.search
string into keys and values.
来源:https://stackoverflow.com/questions/10622619/pass-data-between-pages