在页面之间传递数据

时间:2012-05-16 16:21:02

标签: jquery ajax jquery-mobile cordova

我正在使用phonegap上的移动应用,我需要在页面之间传递变量,如:http://coenraets.org/apps/directory/jqm/index.html

但我的问题是我不能使用php文件。 我怎样才能做到这一点 ? 谢谢:))

我刚试过这里提到的LocalStorage:http://docs.phonegap.com/en/1.6.1/cordova_storage_storage.md.html#localStorage 但它不起作用: 这是我的代码:

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>

但它不起作用。 你有什么想法吗?

2 个答案:

答案 0 :(得分:1)

location.hashlocation.search(查询字符串)中设置变量,然后在第二页上使用JavaScript检索它。

https://developer.mozilla.org/en/DOM/window.location#Properties

请注意,当您检索location.hashlocation.search时,您需要使用.substring(1)之类的内容删除第一个字符(#或?):

var hash = location.hash.substring(1);

另请参阅this question了解将location.search字符串解析为键和值的技巧。

答案 1 :(得分:1)

我很想使用LocalStorage,将数据存储在第一页并在下一页中检索。

然后清除localstorage以防止它被遗忘。

http://docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html#localStorage