Search code examples
jqueryruby-on-railsturbolinkszendesk

Turbolink causing issue to load the 3rd party javascript(zendesk) on next page


I am on rails3, and Turbo-links causes the link for zendesk to not work.

I was using zendesk script and that was not loading on next page and then I have used script that mentioned here. It seems working fine on chrome, but on Firefox and Safari, its still causing problem.

I have included below lines into my layout:

= javascript_include_tag 'zendesk', 'data-turbolinks-track' => true

:javascript
  document.addEventListener('page:change', zendesk_web_widget());

and zendesk_web_widget() is function that loading widget.

I have searched around web and found some solutions but not working at all:

Rails 4 turbo-link prevents jQuery scripts from working

https://github.com/turbolinks/turbolinks-classic/issues/166

Any anyone has any idea then please let me know.


Solution

  • You are passing the return value of the function zendesk_web_widget to the listener, not the function itself. So instead of calling the function (with ()), try passing the function:

    document.addEventListener('page:change', zendesk_web_widget);
    

    Or, making a wrapper function for it:

    document.addEventListener('page:change', function () { zendesk_web_widget(); });
    

    And since you're using jQuery, why not make the code use jQuery.

    $(document).on('page:change', zendesk_web_widget);