Skip to main content
Solved

How to restore Freshchat after window.fcWidget.destroy() call?

  • January 28, 2025
  • 1 reply
  • 25 views

evgen12
Apprentice
Forum|alt.badge.img

According to the documentation (https://developers.freshchat.com/web-sdk/v2/#messenger-api) we can destroy the Freshchat via `window.fcWidget.destroy()` call.

But how to re-initialize the Freshchat again?
It doesn't work via `window.fcWidget.init()`

Best answer by Aparna-narayanan

hi ​@evgen12 

 

To initialize widget post destroy, we can have the initialization inside a method like this and call the function in the call back of the widget:destroyed function.

 



window.fcWidget.on("widget:destroyed", function() {
      console.log('After widget destroyed');
      initWidget();
      // Do some clean-up here 
    });



function initWidget(){
    window.fcWidgetMessengerConfig = {
        // add your config here
    }
    var freshchatScript = document.createElement('script');
    freshchatScript.src = '//fw-cdn/********/****.js'; // add the unified js here
    freshchatScript.setAttribute('chat', 'true');
    //freshchatScript.setAttribute('widgetId', 'widgetUUID');
    freshchatScript.id = 'freshchatScript'; // Assign an id to the script
    freshchatScript.onload = () => {
        window.fwcrm.on('widget:loaded',function(){
        // Add your custom code here
        });
    }
    // Append the script to the head of the document
    var head = document.head || document.getElementsByTagName('head')[0];
    head.insertBefore(freshchatScript, head.firstChild);
}

Hope it helps with ur query shared.

 

Thanks

View original
Did this topic help you find an answer to your question?

1 reply

Aparna-narayanan
Community Debut
Forum|alt.badge.img

hi ​@evgen12 

 

To initialize widget post destroy, we can have the initialization inside a method like this and call the function in the call back of the widget:destroyed function.

 



window.fcWidget.on("widget:destroyed", function() {
      console.log('After widget destroyed');
      initWidget();
      // Do some clean-up here 
    });



function initWidget(){
    window.fcWidgetMessengerConfig = {
        // add your config here
    }
    var freshchatScript = document.createElement('script');
    freshchatScript.src = '//fw-cdn/********/****.js'; // add the unified js here
    freshchatScript.setAttribute('chat', 'true');
    //freshchatScript.setAttribute('widgetId', 'widgetUUID');
    freshchatScript.id = 'freshchatScript'; // Assign an id to the script
    freshchatScript.onload = () => {
        window.fwcrm.on('widget:loaded',function(){
        // Add your custom code here
        });
    }
    // Append the script to the head of the document
    var head = document.head || document.getElementsByTagName('head')[0];
    head.insertBefore(freshchatScript, head.firstChild);
}

Hope it helps with ur query shared.

 

Thanks


Reply