admin管理员组文章数量:1023815
Found pretty good script for showing recently viewed products using sessionStorage. There is only one problem: each product could be stored and showed multiple times.
Here is jsfiddle /
There should be one more IF to check if current product is already in storage, something like this:
// check if product already in storage
if(!productExistsInStorage()) {
var currentCount = parseInt(sessionStorage.getItem("storedCount"));
// add to storage
sessionStorage.setItem(currentCount.toString(), JSON.stringify(productInfo()));
// update storedCount var by one
sessionStorage.setItem("storedCount", (currentCount + 1).toString());
}
I tried to make it, but without any success.
Any tips how not to save product data if it was already set in sessionStorage?
Found pretty good script for showing recently viewed products using sessionStorage. There is only one problem: each product could be stored and showed multiple times.
Here is jsfiddle http://jsfiddle/r77oozh8/
There should be one more IF to check if current product is already in storage, something like this:
// check if product already in storage
if(!productExistsInStorage()) {
var currentCount = parseInt(sessionStorage.getItem("storedCount"));
// add to storage
sessionStorage.setItem(currentCount.toString(), JSON.stringify(productInfo()));
// update storedCount var by one
sessionStorage.setItem("storedCount", (currentCount + 1).toString());
}
I tried to make it, but without any success.
Any tips how not to save product data if it was already set in sessionStorage?
Share Improve this question edited Dec 15, 2015 at 19:10 Toon asked Dec 15, 2015 at 15:43 ToonToon 6521 gold badge11 silver badges24 bronze badges3 Answers
Reset to default 3Just came across this post after I've looked it up myself.
if ("value" in sessionStorage) {
alert("yes");
}
else {
alert("no");
}
Instead of setting separate items in sessionStorage
for each product, i would set them all in one array. That would make it easier to check if it already exists:
var currentItems = JSON.parse(sessionStorage.getItem('products'));
var currentProduct = productInfo();
if (currentItems.filter(function(p){return p.productURL == currentProduct.productURL}).length == 0) {
// add
}
Why not check is session storage for that item is null or not?
if(sessionStorage.getItem("storedCount") === null)
{
//do something
}
Even better make a function that you can pass the name of an item to to check it.
function doesStorageExist(item)
{
if(sessionStorage.getItem(""+item) === null)
{
//do something
}
}
Found pretty good script for showing recently viewed products using sessionStorage. There is only one problem: each product could be stored and showed multiple times.
Here is jsfiddle /
There should be one more IF to check if current product is already in storage, something like this:
// check if product already in storage
if(!productExistsInStorage()) {
var currentCount = parseInt(sessionStorage.getItem("storedCount"));
// add to storage
sessionStorage.setItem(currentCount.toString(), JSON.stringify(productInfo()));
// update storedCount var by one
sessionStorage.setItem("storedCount", (currentCount + 1).toString());
}
I tried to make it, but without any success.
Any tips how not to save product data if it was already set in sessionStorage?
Found pretty good script for showing recently viewed products using sessionStorage. There is only one problem: each product could be stored and showed multiple times.
Here is jsfiddle http://jsfiddle/r77oozh8/
There should be one more IF to check if current product is already in storage, something like this:
// check if product already in storage
if(!productExistsInStorage()) {
var currentCount = parseInt(sessionStorage.getItem("storedCount"));
// add to storage
sessionStorage.setItem(currentCount.toString(), JSON.stringify(productInfo()));
// update storedCount var by one
sessionStorage.setItem("storedCount", (currentCount + 1).toString());
}
I tried to make it, but without any success.
Any tips how not to save product data if it was already set in sessionStorage?
Share Improve this question edited Dec 15, 2015 at 19:10 Toon asked Dec 15, 2015 at 15:43 ToonToon 6521 gold badge11 silver badges24 bronze badges3 Answers
Reset to default 3Just came across this post after I've looked it up myself.
if ("value" in sessionStorage) {
alert("yes");
}
else {
alert("no");
}
Instead of setting separate items in sessionStorage
for each product, i would set them all in one array. That would make it easier to check if it already exists:
var currentItems = JSON.parse(sessionStorage.getItem('products'));
var currentProduct = productInfo();
if (currentItems.filter(function(p){return p.productURL == currentProduct.productURL}).length == 0) {
// add
}
Why not check is session storage for that item is null or not?
if(sessionStorage.getItem("storedCount") === null)
{
//do something
}
Even better make a function that you can pass the name of an item to to check it.
function doesStorageExist(item)
{
if(sessionStorage.getItem(""+item) === null)
{
//do something
}
}
本文标签: javascriptsessionStorage if value already existStack Overflow
版权声明:本文标题:javascript - sessionStorage if value already exist - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745508021a2153696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论