Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

84
Visualizações
Javascript cookie removed after leaving page

I'm attempting to create a cookie the first time a user visits a page on the website and store the pathname as the value of the cookie for that page for records.

I'm attempting on doing this by creating a function at the page load called GenerateCookie.

This function then checks to see if the cookie reportingpage exists or not and if it does not exist create the cookie. I would like the value to remain the same until the session is over. I am attempting to record the first page visited. So for example, if a user visits /testing-page/ I would like the cookie value to remain /testing-page/ even if they back out of the page and visit other pages, since that was the first page visited.

Currently, the cookie is created as expected with the pathname value as expected, but any time I back out of the page and visit a new page the cookie is then removed and set with the other pages pathname value. I've attempted to fix this issue by including the path= attribute when setting the cookie, but this has no effect.

How can I create a cookie and keep that same value for the entire session until the tab/browser is closed?

Here is a code snippet of my code:

  GenerateCookie();


    function GenerateCookie() {

     // if cookie does not exist, create cookie reporting page

        if (document.cookie.indexOf('reportingpage=') == -1) {

            console.log('no cookie')

            document.cookie = "reportingpage=https://www.testing.com" + window.location.pathname + "; path=/"

        } else {

            // if cookie exist, get value
             console.log('cookie exist')

            const name = "reportingpage"

            const match = document.cookie.match(RegExp('(?:^|;\\s*)' + name + '=([^;]*)')); 

            console.log(match[1], 'value')
        }

    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Storing a hundred cookies on someone's computer is really unethical, especially if it's for tracking their page visits. I'd be super annoyed if I visited a site and suddenly have a gazillion cookies to delete. But perhaps you have good reasons so if you really have to do it then use localStorage instead of cookies.

sessionStorage.setItem('someKey', window.location.pathname)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda