0

I want to be sure that my scripts are being loaded before I call a function.

This code is written inline on the page, it is not being loaded by application.js, sometimes the scripts are loaded correctly but not always.

<script>
$(document).on('turbolinks:load', function() {
  $.when(
  // Required libs to token and fraud detection
  $.getScript("https://openpay.s3.amazonaws.com/openpay.v1.min.js"),
  $.getScript("https://openpay.s3.amazonaws.com/openpay-data.v1.min.js"),
  $.Deferred(function(deferred){
      $(deferred.resolve);
  })
).done(function(){
  OpenPay.setId('123');
  ... more code ...
});
</script>

I'm getting:

enter image description here

How can I do that? I'm using rails 5

Carlos Gómez
  • 453
  • 7
  • 16
  • Have you added your application.js to your application.html.erb file or whatever it's name is which contains your meta tags, stuff, and header ()? – Leo Van Deuren Jul 02 '18 at 19:09
  • Yes, but this script is used "inline" to say it in some way. is in the same html that uses it. the problem is that sometimes it loads and sometimes it does not, I think it's because the code inside the function sometimes loads before the library https://openpay.s3.amazonaws.com/openpay.v1.min.js and https://openpay.s3.amazonaws.com/openpay-data.v1.min.js and thats why return ReferenceError, but I do not know how to make me load the libs before the function and that this is always effective so that I do not return the ReferenceError – Carlos Gómez Jul 02 '18 at 21:06
  • I found a related stackoverflow post might be useful. https://stackoverflow.com/questions/31011528/javascript-includes-not-loading-for-later-in-the-page – Leo Van Deuren Jul 03 '18 at 13:32

0 Answers0