turbolinks-compatibility icon indicating copy to clipboard operation
turbolinks-compatibility copied to clipboard

Zendesk turbolinks script doesn't work

Open andrewkouri opened this issue 9 years ago • 7 comments

Turbolinks does not work with the zendesk script contributed. When you navigate away from the current page by clicking on a link, the zendesk widget does not persist between pages.

andrewkouri avatar Mar 03 '15 03:03 andrewkouri

@andrewkouri I had with the same problem, when clicked in any link the zendesk stop of work, 'cause in my case i'm using zopim chat.

To fix my problem, I added 2 new lines. After/before of these lines:

window.zEmbed = window.zE = undefined
$('head iframe[src="javascript:false"]').remove()

I added:

$('[__jx__id], embed#__zopnetworkswf').remove()
window.$zopim = null

leonardostefani avatar May 18 '15 06:05 leonardostefani

@leonardostefani that did for me, thanks! But I've set window.$zopim = undefined;

Now my widget function looks like this:

// reference: http://reed.github.io/turbolinks-compatibility/zendesk_web_widget.html
function zendeskWebWidget(userName, userEmail, enableHelpCenter, enableChat){
  var zendesk_host = 'myhostname.zendesk.com';

  window.zEmbed = window.zE = undefined;
  $('head iframe[src="javascript:false"]').remove();
  // reference: https://github.com/reed/turbolinks-compatibility/issues/52
  $('[__jx__id], embed#__zopnetworkswf').remove()
  window.$zopim = undefined; 

  (function(url, host) {
    var queue = [],
        dom,
        doc,
        where,
        iframe = document.createElement('iframe'),
        iWin,
        iDoc;

    window.zEmbed = function() {
      queue.push(arguments);
    };

    window.zE = window.zE || window.zEmbed;
    window.zESettings = {
      webWidget: {
        helpCenter: {
          suppress: !enableHelpCenter
        },
        chat: {
          suppress: !enableChat
        }
      }
    };

    iframe.src = 'javascript:false';
    iframe.title = ''; iframe.role='presentation';  // a11y
    (iframe.frameElement || iframe).style.cssText = 'display: none';
    where = document.getElementsByTagName('script');
    where = where[where.length - 1];
    where.parentNode.insertBefore(iframe, where);

    iWin = iframe.contentWindow;
    iDoc = iWin.document;

    try {
      doc = iDoc;
    } catch(e) {
      dom = document.domain;
      iframe.src='javascript:var d=document.open();d.domain="'+dom+'";void(0);';
      doc = iDoc;
    }
    doc.open()._l = function() {
      var js = this.createElement('script');
      if (dom) this.domain = dom;
      js.id = 'js-iframe-async';
      js.src = url;
      this.t = +new Date();
      this.zendeskHost = zendesk_host;
      this.zEQueue = queue;
      this.body.appendChild(js);
    };
    doc.write('<body onload="document._l();">');
    doc.close();
  }('//assets.zendesk.com/embeddable_framework/main.js', "myhostname"));

  if(userName != ''){
    zE( function () {
      zE.identify({name: userName, email: userEmail});
    });
  }
}

And on the page header I have:

$(document).on("turbolinks:load", function() {
  var userName = 'username'; // fill it your way
  var userEmail = 'userEmail';
  var enableHelpCenter = false;
  var enableChat = true;

  zendeskWebWidget(userName, userEmail, enableHelpCenter, enableChat);
});

If anyone sees a better way to do this, please help me improve it! Thank you!

MauricioMoraes avatar Oct 04 '17 14:10 MauricioMoraes

 $('[__jx__id], embed#__zopnetworkswf').remove()
 window.$zopim = undefined; 

Made trick! awesome.

rafaltrojanowski avatar Aug 06 '18 08:08 rafaltrojanowski

Its strange how setting window.$zopim to undefined solves the issue

abhinavmsra avatar Aug 08 '18 10:08 abhinavmsra

@MauricioMoraes That worked perfectly. Thanks so much!

zkingdesign avatar Feb 14 '19 11:02 zkingdesign

hey, I was wondering if this still works @MauricioMoraes

Ghostavio avatar Mar 30 '20 10:03 Ghostavio

@Ghostavio we've ditched zendesk/zopim a year ago. So I have no idea if this workaround is still working. But my gut tells me that it should, because those widget inclusion scripts change very little over time (too much caching and compatibility problems to change it), and the main script is loaded from the server. So there is little risk of you wasting your time trying it out.

MauricioMoraes avatar Mar 30 '20 20:03 MauricioMoraes