我需要什么
js code
<script type="text/javascript">
var count=0;
var counter=sessionStorage.setItem("count", 0);
var counters= sessionStorage.getItem("count");
console.log(counters);
var page_count =counters;
if (page_count == 4)
{
dataLayer.push({'event':'mobilePromo-android'});
}
$(document).ready(function()
{
var page_count =counters;
var height= $(window).height();
if (page_count == 4 )
{
$.ajax({
type: "GET",
url: "http://domain.com/mobilepopuptracker?from=android",
});
$('body').html('<div class="row flush aligncenter popbx" style="height:'+height+'px"><img src="http://im.gifbt.com/images/logo-300p");
}
else
{
}
counters=counters+1;
});
function redirect()
{
var a=$(location).attr('href');
window.location.href=a;
}
</script>
问题
在价值解决方案代码仍未运行之后
var counter = null;
if(sessionStorage.getItem("count") == null)
{
counter=sessionStorage.setItem("count", 0);
counters = 0;
}
else
{
counters= parseInt(sessionStorage.getItem("count"));
}
var page_count =counters;
if (page_count == 4)
{
dataLayer.push({'event':'mobilePromo-android'});
}
var page_count =counters;
console.log(page_count);
var height= $(window).height();
if (page_count == 4 )
{
$.ajax({
type: "GET",
url: "http://times.com/mobilepopuptracker?from=android",
});
$('body').html('<div class="row flush aligncenter popbx" style="height:'+height+'px">');
}
else
{
}
counters++;
console.log(counters);
function redirect()
{
var a=$(location).attr('href');
window.location.href=a;
}
输出
参考链接
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
答案 0 :(得分:2)
您需要先检查会话值是否可用
var counter = null;
if(sessionStorage.getItem("count") == null){
counter=sessionStorage.setItem("count", 1);
counters = 1;
}else{
counters= parseInt(sessionStorage.getItem("count"));
counters++;
counter=sessionStorage.setItem("count", counters);
}
答案 1 :(得分:0)
这样的事情应该有效
$(document).ready(function() {
var ls = sessionStorage.getItem("session-count");
if(ls) {
/*Do whatever here*/
var count = sessionStorage.getItem("session-count");
sessionStorage.setItem("session-count", parseInt(count) + 1);
} else {
sessionStorage.setItem("session-count", 1);
}
});