How to remove freshchat window from other pages in Vuejs?

Viewed 1126

I need to remove fresh chat window from all pages except the home page. I put the script inside the home component mounted part. But once it loads it never closes. If I refresh the page it disappears. I think it's because of the SPA. I think once it loads the JS it does not clear the header script.

mounted() {
    function initFreshChat() {
        window.fcWidget.init({
            token: "***",
            host: "https://wchat.freshchat.com"
        });
    }

    function initialize(i, t) {
        var e;
        i.getElementById(t) ? initFreshChat() : ((e = i.createElement("script")).id = t, e.async = !0, e.src = "https://wchat.freshchat.com/js/widget.js", e.onload = initFreshChat, i.head.appendChild(e))
    }

    function initiateCall() {
        initialize(document, "freshchat-js-sdk")
    }
    window.addEventListener ? window.addEventListener("load", initiateCall, !1) : window.attachEvent("load", initiateCall, !1);
}
3 Answers

I did something similar and added it to the useEffect hook in react, just specifying on each page the argument of true if I wanted it hidden.

const addFreshchat = (hiddenOnLoad) => {
  const hideFreshChat = {
    config: {
      headerProperty: {
        hideChatButton: true,
      },
    },
    token: 'your-account-token',
    host: 'https://<freshchatUrl>',
  };

  const showFreshChat = {
    token: 'your-account-token',
    host: 'https://<freshchatUrl>',
  };
  const freshChatConfig = hiddenOnLoad ? hideFreshChat : showFreshChat;

  function initFreshChat() {
    window?.fcWidget?.init(freshChatConfig);
  }

  function initialize(i, t) {
    let e;
    const initiate = i.getElementById(t)
      ? initFreshChat()
      : ((e = i.createElement('script')).id = t, e.async = !0, e.src = 'https://wchat.eu.freshchat.com/js/widget.js', e.onload = initFreshChat, i.head.appendChild(e));
    return initiate;
  }
  function initiateCall() {
    initialize(document, 'freshchat-js-sdk');
  }
  function addSDK() {
    const addListener = window.addEventListener ? window.addEventListener('load', initiateCall, !1) : window.attachEvent('load', initiateCall, !1);
    return addListener;
  }
  addSDK();
};

I just integrated fresh chat in my app:

window.fcWidget.show() // show widget
window.fcWidget.hide() // hide widget
window.fcWidget.init({
      token: 'your token',
      host: 'host'
    }) // initialize
window.fcWidget.destroy() //destroy
Related