Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
Javascript Local storage with function?

I have a translation feature that switches paragraphs from english to french using javascript at the click of a button. Now I manually grab pre-translated paragraphs using innerhtml.I want to store the users action using localStorage. If they click on the translation onclick event, it should automatically run the translate_fr function on every other page. I want to avoid having the user to have to re-translate the website everytime they load a new page.

I have no clue how to do this, with some sort of conditional statement If the user clicks translate_fr the other pages should run a script to repeat that function once they load the other pages and I assume this can be done if their initial choice is stored in the browser using localstorage.

 function translate_fr(){
 
 document.getElementById("intro").innerHTML = `Chez Coupe Lion, nous ne sommes qu'un chat établissement offrant une gamme complète de services du toilettage complet, de la baignade à l'embarquement.Vous et votre animal sera ravi de savoir que seul un professionnel, des produits naturels et biodégradables sont utilisés, tout les sensibilités ou les allergies ne seront pas un problème`;
 
}
<button class="translate" type="button" onclick="translate_fr()">FR</button>

    <p id="intro">
      Here at Lion Kuts we are a cat only
      establishment that offers a full range of services 
      from complete grooming, bathing to boarding. You and 
      your pet will be thrilled to know that only professional, 
      natural and biodegradeable products are used, any 
      sensitivities or allergies will not be a problem.
  </p>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could try something like this:

function translate_fr() {
  document.getElementById(
    "intro"
  ).innerHTML = `Chez Coupe Lion, nous ne sommes qu'un chat établissement offrant une gamme complète de services du toilettage complet, de la baignade à l'embarquement.Vous et votre animal sera ravi de savoir que seul un professionnel, des produits naturels et biodégradables sont utilisés, tout les sensibilités ou les allergies ne seront pas un problème`;
  localStorage.setItem("isFrench", "1");
}

function translate_eng() {
  document.getElementById("intro").innerHTML = `Here at Lion Kuts we are a cat only
  establishment that offers a full range of services 
  from complete grooming, bathing to boarding. You and 
  your pet will be thrilled to know that only professional, 
  natural and biodegradeable products are used, any 
  sensitivities or allergies will not be a problem.`;
  localStorage.setItem("isFrench", "0");
}

function check_translation() {
  const isFrench = localStorage.getItem("isFrench");
  return parseInt(isFrench);
}

function translate() {
  if (check_translation() === 1) {
    translate_fr();
    return;
  }

  translate_eng();
}

translate();

  • Firing a function translate() on load.
  • This could check local storage value localStorage.getItem("isFrench");.
  • Local storage is strings, so converting to an int but not real need to do that.
  • Which would be set when the users does your onClick event (which isn't in this example)
  • If 1, display the French.
  • If 0, display the English.
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda