Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!