如何解决HTML5 LocalStorage:检查密钥是否存在?

引用规范

getItem(key) 方法必须返回与给定键关联的当前值。如果与对象关联的列表中不存在给定键,则此方法必须

您实际上应该检查null.

if (localStorage.getItem("username") === null) {
  //...
}

解决方法

为什么这不起作用?

if(typeof(localStorage.getItem("username"))=='undefined'){
    alert('no');
};

目标是将用户从索引页面重定向到登录页面(如果尚未登录)。这里的localStorage.getItem("username"))变量暂时没有定义。

这是一个 ios phonegap 应用程序。