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

246
Vistas
Initailize a function only once in javascript

I have a custom code to fire my Live chat bot instance if a user visits a particular URL.

if(location.hash.indexOf("#/financial-bills")>-1){
return true; 

The issue is the chats are repeating if I go to child URLs e.g #/financial-bills/summary My chat code is below:

 var initESW = function(gslbBaseURL) {
 embedded_svc.init(
        'https://example.com',
        'https://example/ChatBot',
        gslbBaseURL,
        '00D4C00000019Iy',
        'Support_Only',
        {
        baseLiveAgentContentURL: 'https://c.la2-c1cs.com/content',
        deploymentId: '5721E',
        buttonId: '5731E',
        baseLiveAgentURL: 'https://d.la2-c1cs.com/chat',
        eswLiveAgentDevName: 'EmbeddedServiceLiveAgent_Parent0200EAA_1754b2de11b',
        isOfflineSupportEnabled: false
        }
    );
    };



if (!window.embedded_svc) {
        var s = document.createElement('script');
        s.setAttribute('src', 'https://example/embeddedservice/5.0/esw.min.js');
        s.onload = function() {
        initESW(null);
   };
document.body.appendChild(s);
 } else {
initESW('https://service.force.com');
}

Repeating Chats

My issue is I want the chat function to initialize only once. As I beleive it is causing the chats to repeat. I have tried removing the chat script based on URL change event but its not working for me.

 script.id = "test";
    window.addEventListener('popstate', function (event) {
    (elem=document.getElementById("test")).parentNode.removeChild(elem)
    });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use a temporary session cookie to check if the chat was initialized once or not. If not just initialize the chat and set the cookie.

function setCookie(cname,cvalue) {
  document.cookie = cname+"="+cvalue;
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


if(!getCookie('chatInitialized')){
  initChat();
  setCookie('chatInitialized', true);
}

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