0

I need some help. I'm a beginner on this. My javascript doesn't seem to be loading after a user clicks on a link_to and I think the issue might be compatibility with Turbolinks. Any one can help me on this? Below is my script. Thanks in advance!

$(document).ready(function() {
  // executes when HTML-Document is loaded and DOM is ready
  console.log("document is ready");

  $(".navbar-nav").clone().prependTo("#off-canvas");

  $(function() {
    $(document).trigger("enhance");
  });

  // document ready  
});

$(window).load(function() {
  // executes when complete page is fully loaded, including all frames, objects and images
  console.log("window is loaded");

  // window load  
});

This solution suggested in another question doesn't work for me at all;

$(document).on('turbolinks:load', function() {
jacobrubyonrails
  • 147
  • 1
  • 10
  • Possible duplicate of [Rails 4: how to use $(document).ready() with turbo-links](https://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links) – jvillian Jan 20 '18 at 17:53
  • Hi jvillian, possibly. But the accepted answer/solution seems to rely on the javascript turbolinks gem (https://github.com/kossnocorp/jquery.turbolinks) which is deprecated. – jacobrubyonrails Jan 20 '18 at 17:57
  • Ignore accepted answer. See, for example, answers from Meltemi or vedant1811 (above accepted answer). – jvillian Jan 20 '18 at 18:05

1 Answers1

0

try changing the line: (document).ready(function() {

to: $(document).on('turbolinks:load', function() {

if you have already tried that and it does not work, you may need to reinitialize/load the js in the final js.erb file.

You can do this a few ways. Here is one:

Attach your javascript to the window so that it is globally available.

Call that function in your js.erb like this: Global.some_js_function();

dstull
  • 338
  • 1
  • 9