Manual call document.ready

Is there a way to get the document.ready event to fire through javascript? The new default Rails 4 using turbo links by default kills this firing event when switching pages. This causes problems with the more "traditional" jQuery code, which is wrapped in $(function(){}); .

I looked around and see no way to make this event fire through code, and most of the answers here offer other work.

So is it even possible to make document.ready fire through regular javascript code?

+4
source share
4 answers

Do you use jQuery? If so, try explicitly calling the jQuery function $.ready(); at the end of your code. This will make all jQuery ready-made functions be called, both your own and those that you use in any plug-ins that you use.

If this does not work, you can try $(window).load(); .

If none of them work and you use some kind of asynchronous call, you probably have another problem to knock the first out of it. Good luck

+3
source

I think that the easiest way to do this without big changes in your code is to listen to a custom event, rather than ready to fire a custom event inside a finished event.

+1
source

From the turbolinks documentation : You must make the document listen for one of the events on the turbolinks user page, for example:

 $(document).on('page:change', function() { // Your code }); 
+1
source

The simplest solution:

Paste the gemfile into this:

 gem 'jquery-turbolinks', '~> 1.0.0' 

Insert your application application.js:

 //= require jquery.turbolinks 

(Don't forget a bundle update )

If you have coffescript code, it will be launched to completely reload the page or to open the page using the link when turbolinks is turned on:

 $ -> alert 'Hello World' 

( $ -> means document.ready )

+1
source

Source: https://habr.com/ru/post/1490003/


All Articles