0

I have the following code:

<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.js"></script>
<script type="text/javascript">
document.addEventListener("turbolinks:load", function () {
  $('#myTable').DataTable();
});
</script>

It uses DataTables in my table with id id=myTable But upon the first load after rails s, the console logs:

Uncaught TypeError: $(...).DataTable is not a function
    at HTMLDocument.<anonymous> (<anonymous>:3:17)
    at Object../node_modules/turbolinks/dist/turbolinks.js.e.dispatch (turbolinks.js:75)
    at r.notifyApplicationAfterPageLoad (turbolinks.js:994)
    at r.visitCompleted (turbolinks.js:1007)
    at r.complete (turbolinks.js:782)
    at r.<anonymous> (turbolinks.js:812)
    at turbolinks.js:862

After I refresh, the error is gone, and the script (kind of) works. It does what it is supposed to, but it might render a duplicate or triplicate if I go back one page to the page that renders the script.

I think Uncaught TypeError: $(...).DataTable is not a function is because my script was loaded before webpacker, maybe, and that is why it won't work until I refresh.

So I tried to copy the txt from src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.js" and created a "datatables.js" file in my javascript/js/ folder, as such: ![datatables.js location]

Afterwards, imported it to the application.js webpacker:

// app/javascript/packs/application.js
import "../js/datatables"

and in my genes.html.erb (where I have my DataTables script), removed the CDN script, as it should've been imported via webpacker:

<script type="text/javascript">
document.addEventListener("turbolinks:load", function () {
  $('#myTable').DataTable();
});
</script>

and the console logs the same error:

Uncaught TypeError: $(...).DataTable is not a function
    at HTMLDocument.<anonymous> (<anonymous>:3:17)
    at Object../node_modules/turbolinks/dist/turbolinks.js.e.dispatch (turbolinks.js:75)
    at r.notifyApplicationAfterPageLoad (turbolinks.js:994)
    at r.visitCompleted (turbolinks.js:1007)
    at r.complete (turbolinks.js:782)
    at r.<anonymous> (turbolinks.js:812)
    at turbolinks.js:862

but it doesn't go away after a refresh.

Any ideas?

EDIT 1:

Following tanner2379's directions

I tried to add this at the bottom of my application.js file to have it reload if turbolinks wasn't loaded

// app/javascript/packs/application.js
Rails.start()
Turbolinks.start()
ActiveStorage.start()


if (!Turbolinks) {
    location.reload();
  }
  Turbolinks.dispatch("turbolinks:load");

and this at the bottom of my genes.html.erb file to prevent it from loading multiple times

<script type="text/javascript">
document.addEventListener("turbolinks:load", function () {
  $('#myTable').DataTable();
  event.stopImmediatePropagation();
  return false;
});
</script>

but the problem is still the same. It doesn't load on first arrival and it instantiates multiple times if I go back to the page, as such

EDIT 2:

Tried arieljuod's reply

<!-- in genes.html.erb -->
<script>
document.addEventListener('turbolinks:load', () => {
  $(() => {
    $('#myTable').DataTable();
  })
})
</script>

but didn't work either - same error.

Tried Pointy's reply

added this code to my gene.html.erb:

<script>
document.addEventListener("turbolinks:load", function () {
  var script = document.createElement('script');
  script.onload = function() {
    alert("Script loaded and ready");
  };
  script.src = "https://cdn.datatables.net/1.10.24/js/jquery.dataTables.js";
  document.getElementsByTagName('head')[0].appendChild(script);
});
</script>

<script type="text/javascript">
document.addEventListener("turbolinks:load", function () {
  $('#myTable').DataTable();
 
});
</script>

but it doesn't work. Despite receiving the alert from localhost:3000 saying Script loaded and ready, DataTables doesn't show - Console logs:

Uncaught TypeError: $(...).DataTable is not a function
    at HTMLDocument.<anonymous> (genes:168)
    at Object../node_modules/turbolinks/dist/turbolinks.js.e.dispatch (turbolinks.js:75)
    at r.notifyApplicationAfterPageLoad (turbolinks.js:994)
    at r.pageLoaded (turbolinks.js:948)
    at turbolinks.js:872

I have also tried adding the script to the application.js webpacker (same error)

EDIT 4?:

<script type="text/javascript" charset="utf8">
 var script = document.createElement('script');
 script.onload = function() {
   alert("Script loaded and ready");
 };
 script.async = true;
 script.src = "https://cdn.datatables.net/1.10.24/js/jquery.dataTables.js";

// MODIFIED THE SCRIPT TO ADD IT TO "BODY" INSTEAD, as in the console, the "working" version has it in the body tag
 document.getElementsByTagName('body')[0].appendChild(script);
 </script>
<script type="text/javascript">
document.addEventListener("turbolinks:load", function () {
  $('#myTable').DataTable();
 
});
</script>

Works and appends the script to <head> but DataTables won't load (not a function). I don't know what's the difference between loading it like this, which doesn't work, and this one that does(after a refresh):

<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.js"></script>

They actually show up in the console in the same place, within the <body> tag

cratag
  • 112
  • 12

2 Answers2

1

What I used to do when using jquery and turbolinks together was something like:

document.addEventListener('turbolinks:load', () => {
  $(() => {
    $('#myTable').DataTable();
  })

  // js code not using jquery
})
arieljuod
  • 15,460
  • 2
  • 25
  • 36
0

Put the following code at the bottom of your "turbolinks:load" listener to stop the multiple firing problem.

event.stopImmediatePropagation();
return false;

At the end of your .js file, outside of your listener, put the following to reload the page if turbolinks doesn't load, which it has a habit of not doing on your first page visit.

if (!Turbolinks) {
  location.reload();
}

Turbolinks.dispatch("turbolinks:load");
tanner2379
  • 168
  • 1
  • 2
  • 9
  • Thank you, and sorry, it didn't work - same issue. Maybe I didn't understand correctly. I've updated my pist – cratag Mar 26 '21 at 15:17