使用LocalStorage检测重新访问者

时间:2018-04-20 16:24:41

标签: javascript html cookies local-storage

我知道我们可以使用Cookie检测重新访问者。但是,有没有办法仅使用LocalStorage进行检测?

这就是我的开始。我有一个名为" ifVisited"的LocalStorage对象,其值将更改为" true"仅当用户第二次访问时。

<html>
<body>
<!–– Some code ––>
</body>

<script>
localStorage.setItem('ifVisited', 'false');
var cat = localStorage.getItem("ifVisited");


<!-- **How do I detect if the user is re-visiting?** -->

if(visit == 1) 
{message=" Visit #1!";
var cat = localStorage.getItem("ifVisited");
<!-----------------Do Nothing------------------>
}
if(visit == 2) 
{
localStorage.setItem('ifVisited', 'true');
var cat = localStorage.getItem("ifVisited");

message=" 2nd Visit, Push Promo"
<!-----------------Push Promo------------------>

}
if(visit > 2) 
{
message=" Visit #3!";
var cat = localStorage.getItem("ifVisited");
age.setItem('ifVisited', 'false');
<!-----------------Do Nothing------------------>
}
</script>
</html>

2 个答案:

答案 0 :(得分:2)

[(ngModel)]="modelAccuracy"

答案 1 :(得分:1)

使用localstorage下面的访客计数器示例

<html>

<body>
    <div id="container"></div>
</body>
<script>
    function displayCounter() {
        // check if the localStorage object is supported by the browser
        if ('localStorage' in window && window['localStorage'] !== null) {
            // if the counter has been defined, increment its value, // otherwise, set it to 0
            ('counter' in localStorage && localStorage['counter'] !== null) ? localStorage['counter']++ : localStorage['counter'] = 0;
            var container = document.getElementById('container');
            if (!container) { return };
            // display the counter on screen
            container.innerHTML = 'Hey, you visited this page ' + localStorage['counter'] + ' times.';
        }
    }
    // call the 'displayCounter()' function when the web page is loaded
    window.onload = function () {
        displayCounter();
    }
</script>

</html>