How to perform function once per visitor after loading the site?

Asked

Viewed 130 times

1

How to make Javascript run only once per visitor (type if already registered in the browser’s cokkies, no longer run).

<?php
     $tmp='<script>
     Push.create("Olá Mundo!", {
                      body: "Esta é uma mensagem nova",
                      icon: "images/logo.jpg",
                      timeout: 4000,
                      onClick: function() {
                               window.location="http://www.google.com";
                               this.close();
                      }
                  });
            </script>';

            echo $tmp;
?>
  • cookie recorded by who? php? javascript?

  • javascript itself.

  • and as you set the cookie?

  • friend I’m trying but I couldn’t do.

  • You are using PHP because you do not create and check the cookie with PHP. Hence it only gives echo $tmp; if the cookie does not exist or has expired

  • Thank you very much! Solved.

  • I posted a reply based on the comment, and read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

Show 2 more comments

3 answers

1

You are using PHP, because it does not create and check the cookie with PHP. Hence it only gives the echo $tmp; if the cookie does not exist or has expired

<?php
$tmp='<script>
    Push.create("Olá Mundo!", {
        body: "Esta é uma mensagem nova",
        icon: "images/logo.jpg",
        timeout: 4000,
        onClick: function() {
            window.location="http://www.google.com";
            this.close();
        }
    });
</script>';


$biscoito=$_COOKIE[acessada];

if(empty($biscoito)){

   setcookie( "acessada", "sim", strtotime( '+1 days' ), '/' );  // 24 horas

    echo $tmp;

}
?>

cookies - php

Empty - php

0

I would do using the new localStorage of HTML 5, it is very simple and I believe that all modern browsers already have:

<script>
// escrevendo
localStorage.setItem("ja_acessou", "Sim");
</script>

<script>
// Lendo
if (localStorage.ja_acessou) console.log('já acessou!');
</script>

Example number of accesses:

<script>
if (localStorage.qt_acessos) localStorage.qt_acessos += 1;
else localStorage.qt_acessos = 1;

</script>
  • Thanks! But Alexandre in which part of this script would prevent a new access? I want to show the notification only 1 time a day.

  • You only need to read if (localStorage.ja_accessed) console.log('already accessed! '); and then make the decision you need (redirect etc.), remembering that any solution you find can be undone if the user clears the data from the browser cache (either localStorage or cookie).

  • to control if you have accessed 1 time per day, store the date of the last access in localStorage.

0

I particularly like to wear cookies for this case, example:

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function showUmaVesPorDia() {
    var cooki = getCookie("msgCookie");
    if (cooki != "") {
        // ja existe o cookie msgCookie!
    } else {
           // não existe, vamos guardar!
           $("#mensagem").fadeIn('slow');       
           setCookie("msgCookie", 1, 1);
    }
}
$(document).ready(function(){
        showUmaVesPorDia() ;
});

Here’s an example working http://jsfiddle.net/h55rxehe

Browser other questions tagged

You are not signed in. Login or sign up in order to post.