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

139
Visualizações
Get first element of a key using localStorage.getItem

I was reading the country of the visitor from local storage using localStorage.getItem I was reading this info directly form Google CMP.

console.log("Country code: " + localStorage.getItem('vpb-location'));
var countrycodeckeck = localStorage.getItem('vpb-location');

if (
    countrycodeckeck !== "PH" && //philippines
    countrycodeckeck !== "TH" //thailand
) {

The problem is that now Google changed the key from vpb-location to vpbg and now contains more info.

Example:

var countrycodeckeck = localStorage.vpbg;
console.log(countrycodeckeck);

{"countryCode":"PL","isEU":true,"ts":1641125302501}

Can you please tell me how to modify our code to check the “countryCode” stored in the local storage?

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

0

The data in the storage key is encoded as JSON. To access the information you need to decode the JSON to usable JavaScript with JSON.parse().

// Get the data from the storage.
const vpbg = localStorage.getItem('vpbg');

// Only proceed of there is actually data.
if (vpbg !== null) {
  // Parse the JSON to an object.
  const data = JSON.parse(vpbg);
  
  // data is now an object with properties that you can access.
  const countryCodeCheck = data.countryCode;

  if (
    countryCodeCheck !== "PH" && //philippines
    countryCodeCheck !== "TH" //thailand
  ) {
    // Do something if it checks out.
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You further append it with a dot to access those properties:
localStorage.vpbg.countryCode

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