Phonegap - 本地存储不工作 - Android [英] Phonegap - Local storage not working - Android

查看:170
本文介绍了Phonegap - 本地存储不工作 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用本地存储在网页之间传递值,以创建滚动效果(用户点击链接,并根据ID滚动到网页的特定部分)

I'm using Local Storage to pass values between pages in order to create a scroll to effect (user clicks link and is scrolled to particular part of the page based on ID)

我之前使用的是cookie,但在Android上似乎没有用,我看到本地存储是支持的,所以切换到那。它在浏览器中工作完全正常,但作为一个原生应用程序打包后,我失去了所有的功能?

I was previously using cookies but that didn't seem to work on Android, I read that local storage was supported so switched over to that. It works completely fine when in the browser but as soon as its packaged as a native app I lose all functionality? The API states that it should be supported, any ideas?

这是我的代码:

基本URL:

var storage = window.localStorage;
$("a.scroll_link").click(function(event) {
    event.preventDefault();
    var value = $(this).attr("id");

        storage.setItem("key",value);

    console.log(value);

    window.location=$(this).attr("href");
});

接收网址:

$(function () { 

var value = window.localStorage.getItem("key");

if (value != "" && value != "undefined" && value != null) {
    var storage = window.localStorage;
    storage.setItem("key",value);
    var scroll_type = "";

    if ($.browser.webkit) {
        scroll_type = "body";
    } else {
        scroll_type = "html";
    }

    $(scroll_type)
        .stop()
        .animate({
        //get top-position of target-element and set it as scroll target
        scrollTop: ($("#" + value).offset().top - 25)
        //scrolldelay: 1.5 seconds
    }, {
        duration: 1500,
        complete: function () {
            storage.removeItem("key");
        },
    });
  }
});

代码在浏览器中运行正常,不是本机的,任何想法?

The code works fine in the browser just not natively, any ideas?

感谢,

推荐答案

使用document.addEventListener(deviceready,onDeviceReady,false) $(function(){...}

Use document.addEventListener("deviceready", onDeviceReady, false) instead of $(function(){...}

http://docs.phonegap.com/en/2.5.0/cordova_events_events.md.html#deviceready

这篇关于Phonegap - 本地存储不工作 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆