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

400
Vistas
How to unify 2 scripts of the same type, in order not to create conflict?

I'm using the following javascript to type portions of code basing on the visitors' browser language.

But I've noticed that the fact that both JS use the same commands (just different conditions) cause a conflict, so only the first listed one will work correctly.

How can I solve this?

<script>
const modifyElHTML = (elID, phrases) => {
const el = document.getElementById(elID);
let phrase;
if (italian) {phrase = phrases.italian; }
else if (french) {phrase = phrases.french; }
else if (german) {phrase = phrases.german; }
else {phrase = phrases.english; }
el.innerHTML = `${phrase}`;  };
modifyElHTML('comentario', {italian: 'Scrivi un commento...', french: 'Ajouter un commentaire...', german: 'Kommentieren ...', english: 'Write an answer...',});
</script>   
    
<script>
const modifyElHTML = (elID, phrases) => {
const el = document.getElementById(elID);
let phrase;
if (italian) {phrase = phrases.italian; }
else if (french) {phrase = phrases.french; }
else if (german) {phrase = phrases.german; }
else {phrase = phrases.english; }
el.innerHTML = `<button class="_1gl3 _4jy0 _4jy3 _517h _51sy _42ft" type="submit" value="1"><span>${phrase}</span></button><a href="#" onclick="exit_alert()"></a>`;  };
modifyElHTML('showmorecomments', {
italian: 'Carica altri 10 commenti', french: 'Chargez 10 autres avis', german: '10 weitere Kommentare laden', english: 'Load 10 more comments',});
</script>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you're just worried about conflicting variable names, you can use a closure... or in JS lingo, an IIFE.

(function(){
  const modifyElHTML = (elID, phrases) => {
  const el = document.getElementById(elID);
  let phrase;
  if (italian) {phrase = phrases.italian; }
  else if (french) {phrase = phrases.french; }
  else if (german) {phrase = phrases.german; }
  else {phrase = phrases.english; }
  el.innerHTML = `${phrase}`;  };
  modifyElHTML('comentario', {italian: 'Scrivi un commento...', french: 'Ajouter un commentaire...', german: 'Kommentieren ...', english: 'Write an answer...',});
})();

(function(){
  const modifyElHTML = (elID, phrases) => {
  const el = document.getElementById(elID);
  let phrase;
  if (italian) {phrase = phrases.italian; }
  else if (french) {phrase = phrases.french; }
  else if (german) {phrase = phrases.german; }
  else {phrase = phrases.english; }
  el.innerHTML = `<button class="_1gl3 _4jy0 _4jy3 _517h _51sy _42ft" type="submit" value="1"><span>${phrase}</span></button><a href="#" onclick="exit_alert()"></a>`;  };
  modifyElHTML('showmorecomments', {
  italian: 'Carica altri 10 commenti', french: 'Chargez 10 autres avis', german: '10 weitere Kommentare laden', english: 'Load 10 more comments',});
})();
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can add a parameter to show a button or a text

let lang;
// lang = "italian"; // shorter than italian = true etc

const modifyElHTML = (elID, phrases, button) => {
  const el = document.getElementById(elID);
  const phrase = phrases[lang||"english"]; // added default language if lang not set
  
  el.innerHTML = button ?  `<button class="_1gl3 _4jy0 _4jy3 _517h _51sy _42ft" type="submit" value="1"><span>${phrase}</span></button><a href="#" onclick="exit_alert()"></a>` : `${phrase}`;
};

modifyElHTML('comentario', {
  italian: 'Scrivi un commento...',
  french: 'Ajouter un commentaire...',
  german: 'Kommentieren ...',
  english: 'Write an answer...',
});


modifyElHTML('showmorecomments', {
  italian: 'Carica altri 10 commenti',
  french: 'Chargez 10 autres avis',
  german: '10 weitere Kommentare laden',
  english: 'Load 10 more comments',
}, true); // button = true
<div id="comentario"></div>
<div id="showmorecomments"></div>

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