问题
Data is fetched from web service in variable. How to store the data with white spaces ex:"Bread Butter" in the local storage?
In the function selected_index I have passed the item_name of the clicked item. It leaves the word after space and does not store it.
This is my code.
<script>
var sub_catidall = [];
var sub_catnameall = [];
function selected_index(sub_cat_name_all) {
alert("selected sub category" + sub_cat_name_all);
//window.localStorage.setItem("current_sub_id_all",sub_cat_id_all);
window.localStorage.setItem("DishName", sub_cat_name_all);
window.open("MenuDetails.html", "_self");
}
function jsondata(data) {
var parsedata = JSON.parse(JSON.stringify(data));
var sub_category = parsedata["Item List"];
for (var i = 0; i < sub_category.length; i++) {
var sub_menuall = sub_category[i];
sub_catidall = sub_menuall['menuItemId'];
sub_catnameall = sub_menuall['menuItemName'];
//alert(sub_catnameall);
var id = document.createElement("table");
if ((i % 2) == 0) {
id.innerHTML += '<br><td><a href="#" id=' + sub_catnameall + ' onclick="selected_index(this.id)"><strong><font face="DEVROYE">' + sub_menuall['menuItemName'] + '</a></td></strong>';
document.getElementById("block_left").appendChild(id);
} else {
id.innerHTML += '<br><tr><td><a href="#" id=' + sub_catnameall + ' onclick="selected_index(this.id)"><strong><font face="DEVROYE">' + sub_menuall['menuItemName'] + '</a></td></strong>';
document.getElementById("block_right").appendChild(id);
}
}
}
jsonp("http://remoteaddress/hotelTab/menuitem.php?callback=jsondata&mCatId=" + window.localStorage.getItem('current_id') + "&menuCategoryId=" + window.localStorage.getItem('current_sub_id'));
</script>
回答1:
DOM Storage Guide
Example
var string = 'Bread Butter';
localStorage.setItem("DishName", string);
alert(localStorage.getItem('DishName'));
On jsFiddle
来源:https://stackoverflow.com/questions/22004591/how-to-save-data-with-white-spaces-in-local-storage-using-javascript-in-html5