Manipulate cookie for Popup

Asked

Viewed 148 times

0

Hello, I’m trying to do a popup, I’ve already set up its structure, I just need to do the validation so it doesn’t keep opening every time I visit the page, I’m trying to use cookie, I don’t know if my code is conflicted in wp or is wrong.

<script>
function setCookie(name,exdays){    //função universal para criar cookie
var expires;

var date; 

var value;
date = new Date(); //  criando o COOKIE com a data atual
date.setTime(date.getTime()+(exdays*24*60*60*1000));
expires = date.toUTCString();
value = "TESTE123";
document.cookie = name+"="+value+"; expires="+expires+"; path=/";
}

function getCookie(){
var c_name = document.cookie; // listando o nome de todos os cookies
    if(c_name!=undefined && c_name.length > 0){ // verificando se o mesmo existe
    var posCookie = c_name.indexOf(cookieSeuNome); // checando se existe o cookieSeuNome 
        if (posCookie >= 0){ //se existir o cookie mostra um alert no browser
        document.getElementById('Home_PopBg').style.display = 'none';
        }else{}
    }
}

</script>

1 answer

0

I could not using Js, I did it with php. If someone needs follow the condigo:

<?php
$nome = "popupNews2";
$valor = "style=display:none;";
$expira = time() + 24*3600;
setcookie($nome, $valor, $expira);
$ler = $_COOKIE['popupNews2'];
?>

<section id="Home_PopBg" class="Home_PopBg2" <?php echo $ler; ?>>

Browser other questions tagged

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