reed / turbolinks-compatibility

A collection of tutorials for how to get your favorite javascript libraries, plugins, and snippets working with Turbolinks.
reed.github.io/turbolinks-compatibility
MIT License
147 stars 35 forks source link

Zendesk turbolinks script doesn't work #52

Open andrewkouri opened 9 years ago

andrewkouri commented 9 years ago

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.

leonardostefani commented 9 years ago

@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
MauricioMoraes commented 6 years ago

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

rafaltrojanowski commented 6 years ago
  $('[__jx__id], embed#__zopnetworkswf').remove()
  window.$zopim = undefined; 

Made trick! awesome.

abhinavmsra commented 6 years ago

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

zkingdesign commented 5 years ago

@MauricioMoraes That worked perfectly. Thanks so much!

Ghostavio commented 4 years ago

hey, I was wondering if this still works @MauricioMoraes

MauricioMoraes commented 4 years ago

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