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

250
Visualizações
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 Respostas
Responde à pergunta

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 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