localStorage不支持谷歌浏览器

时间:2015-04-10 11:36:10

标签: javascript jquery html google-chrome firefox

我使用浏览器localStorage来存储值,但在使用Google Chrome时,当我们使用window.location.reload()刷新页面时,localStorage.value会被刷新。例如

localStorage.value1=true

重新加载后,我没有在localStorage中获取此value1对象。

相同的代码适用于mozila firefox,但不适用于chrome。 使用firefox时,localstorage值是持久的。

4 个答案:

答案 0 :(得分:6)

LocalStorage仅支持字符串值,而不支持布尔值或其他值。

存储和检索值的方法:

将值存入存储

localStorage.setItem('value1', 'true');

从存储中检索值

var val1 = localStorage.getItem('value1');

Read more on MDN ..

答案 1 :(得分:1)

您需要使用正确的API for Storage

window.localStorage.setItem('value1', 'true');

您正在做的是在变量上设置属性,该变量在页面加载之间不会持续存在。 Firefox可能过于聪明,并且认识到您希望将该值实际保存在浏览器的本地存储中。

答案 2 :(得分:1)

// main.go
package main

import (
    "encoding/json"
    "fmt"
    "log"
    "io/ioutil"
    "net/http"
    "github.com/gorilla/mux"
)

// Article - Our struct for all articles
type Article struct {
    Id      string    `json:"Id"`
    Title   string `json:"Title"`
    Desc    string `json:"desc"`
    Content string `json:"content"`
}

var Articles []Article

func homePage(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Welcome to the HomePage!")
    fmt.Println("Endpoint Hit: homePage")
}

func returnAllArticles(w http.ResponseWriter, r *http.Request) {
    fmt.Println("Endpoint Hit: returnAllArticles")
    json.NewEncoder(w).Encode(Articles)
}

func returnSingleArticle(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    key := vars["id"]

    for _, article := range Articles {
        if article.Id == key {
            json.NewEncoder(w).Encode(article)
        }
    }
}


func createNewArticle(w http.ResponseWriter, r *http.Request) {
    // get the body of our POST request
    // unmarshal this into a new Article struct
    // append this to our Articles array.    
    reqBody, _ := ioutil.ReadAll(r)

    var article Article 
    json.Unmarshal(reqBody, &article)
    // update our global Articles array to include
    // our new Article
    Articles = append(Articles, article)

    json.NewEncoder(w).Encode(article)
}

func deleteArticle(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    id := vars["id"]

    for index, article := range Articles {
        if article.Id == id {
            Articles = append(Articles[:index], Articles[index+1:]...)
        }
    }

}

func handleRequests() {
    myRouter := mux.NewRouter().StrictSlash(true)
    myRouter.HandleFunc("/", homePage)
    myRouter.HandleFunc("/articles", returnAllArticles)
    myRouter.HandleFunc("/article", createNewArticle).Methods("POST")
    myRouter.HandleFunc("/article/{id}", deleteArticle).Methods("DELETE")
    myRouter.HandleFunc("/article/{id}", returnSingleArticle)
    log.Fatal(http.ListenAndServe(":10000", myRouter))
}

func main() {
    Articles = []Article{
        Article{Id: "1", Title: "Hello", Desc: "Article Description", Content: "Article Content"},
        Article{Id: "2", Title: "Hello 2", Desc: "Article Description", Content: "Article Content"},
    }
    handleRequests()
}

答案 3 :(得分:0)

试试吧

window.localStorage.setItem("value1", true);

var yourvar = window.localStorage.getItem("value1");