언어 설정

Menu
Sites
Language
I'm trying to figure out what would be the best way to create a "ADD TO FAVORITES" function for a Jquery mobile app.

Hi,

I'm trying to figure out what would be the best way to create a "ADD TO FAVORITES" function for a Jquery mobile app.

I looked into HTML5 local storage however most of the examples I have found only work with only one value.

$("#add_favourites").click(function(){      
         var tit_stor = document.getElementById("titlee").innerHTML;
         if (typeof(Storage) !== "undefined") {
                localStorage.setItem(id, tit_stor);
            } else {
                document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
            }    
     }); 

 

Thanks and regards

Mohit Kumar

 

 

 

답변 바로가기

Responses

4 댓글
Mark as answer
Marco Buettner
$("#add_favourites").click(function(){      
    var tit_stor = document.getElementById("titlee").innerHTML;

    if (typeof localStorage !== "undefined") {
        var storageData = localStorage.getItem(id);
        
        if(storageData) { // data already exist on the same location 
            storageData = JSON.parse(storageData);
        } else {
            storageData = [];
        }
        
        storageData.push(tit_stor);
        localStorage.setItem(id, JSON.stringify(storageData));
    } else {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
    }
});

I use an array to save multiple items on the same "save point"...

Marco Buettner

Some details to localStorage. The idea of localStorage is to save key-value-pair. It should not use as "local database", for that u have can use indexedDB. localStorage can also save ONLY strings.

Seoghyun Kang

Dear Mobit Kumar,

 

As Marco's comment, it is also good way to use the database.

If you need to manage the large data, I recommand you use the database.

 

Please refer the following pages.

- https://developer.tizen.org/documentation/articles/indexeddb?langredirect=1
- http://www.w3.org/TR/IndexedDB

 

Thanks.

 

 

 

Seoghyun Kang

If you use the folllowing code, you can check whether the device supports the database.

if (window.openDatabase) {
    // TODO (WebSQL)
}
else if (window.indexedDB) {
    // TODO (Indexed DB)
} 
else {
    // TODO (No DB)
}

 

And if you want to know more information, please refer the following urls.


- https://developer.tizen.org/dev-guide/2.3.0/org.tizen.mobile.web.appprogramming/html/guide/w3c_guide/storage_guide/indexed_database.htm

- https://developer.tizen.org/dev-guide/2.3.0/org.tizen.mobile.web.appprogramming/html/guide/w3c_guide/storage_guide/web_sql_database.htm