在页面之间传递数据 [英] Pass data between pages
问题描述
我正在使用phonegap上的移动应用程序,并且我需要在页面之间传递变量,例如: http://coenraets.org/apps/directory/jqm/index.html
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
但是我的问题是我不能使用php文件. 我怎样才能做到这一点 ? 谢谢:)
but my problem is that I can't use a php file. How can I do that ? Thank you :)
我刚刚尝试了此处提到的LocalStorage: http: //docs.phonegap.com/en/1.6.1/cordova_storage_storage.md.html#localStorage 但它不起作用: 这是我的代码:
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 :
第1页:
$(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++ ;
}
}
});
第2页:
<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 ?
推荐答案
在location.hash
或location.search
(查询字符串)中设置变量,然后在第二页上使用JavaScript进行检索.
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
别忘了,当您检索location.hash
或location.search
时,您会想使用.substring(1)
之类的东西来去除第一个字符(#或?):
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);
另请参阅此问题,以了解解析location.search
字符串的技术键和值.
Also see this question for techniques to parse a location.search
string into keys and values.
这篇关于在页面之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!