Jquery does not run on the first page load!

Asked

Viewed 123 times

0

Hello,

I have a page where the first load of it Jquery is not executed, when updating the page (refresh, F5) the function written in Jquery now works.

I’m using Jquery with technology Ruby On Rails.

Solutions:

  • Add script at end of HTML.
  • Removal of Turbolinks from application.html.erb
  • Add function in application.js

NOTE: However, none of the above solutions worked.

Part of the Jquery function:

$(document).ready(function() {
    request_guides();
});


function request_guides() {
    //logica
}

It was also made that way

$(function() {
    request_guides();
});


function request_guides() {
    //logica
}

Someone has an idea of a solution ?

Grateful!

  • This seems more like a browser error when loading jQuery. Is the connection slow? What do the browser tools say about the jQuery load? How you load it on the page?

2 answers

1

Solution applied based on the answer described in this topic, applying the appropriate changes for use with Ruby On Rails, without the need to add the scripts at the end of body.

Solution adapted for Jquery:

$(document).on('turbolinks:load', function() {
    //Logica
});

0


Try to do it with pure javascript, see what happens:

document.addEventListener('DOMContentLoaded', function(){ 
    request_guides();
}, false);

If it doesn’t work out, let me know to remove the answer.

  • The way was exactly this, but as I am using Rails the turbolinks was interfering and so I applied this solution adapting to Jquery: $(Document). on('turbolinks:load', Function() { //logica });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.