1

I have a simple question. Here is my situation:

Each time that I show an especific page, I call this JQuery function:

<script text="text/>JavaScript">
  $(window).on('load', function(){
    $('body').addClass('background-image')
  });
</script>

That set a background image in my page. It work fine when I load/refresh the page, but if I redirect to this page by a link, the JQuery function is not called. For instance: I load the home page, the image is set; I click in a link to visit the login page, and I click in a link again who redirect me to home page, but the image is not set.

How can I do to call my JQuery function every time the page is shown?

rwehresmann
  • 1,108
  • 2
  • 11
  • 27
  • 1
    `but if I redirect to this page by a link, the JQuery function is not called` - that doesn't sound right at all - clicking a link "loads" a page – Jaromanda X Nov 18 '16 at 00:02
  • 1
    You can use jQuery ready() function, if load event binding is not working OR put your event binding code inside jQuery ready() function – Vikas Sachdeva Nov 18 '16 at 00:03
  • Yes @JaromandaX, is really odd. – rwehresmann Nov 18 '16 at 00:35
  • Put the event binding inside ready function worked @VikasSachdeva . The question is answered, however, something worng is really happening here, because now I have the same situation, calling other function. And for this new case, even put in ready function didn't worked. – rwehresmann Nov 18 '16 at 00:35
  • @rwehresmann OK, I will put this as answer. Regarding your other query, can you raise a new question and describe it there. – Vikas Sachdeva Nov 18 '16 at 01:01
  • I understand, in parts, my problem now, @VikasSachdeva. I'm using Rails 5 framework in my application, with a lib called turbolinks: onde of its effects is not reload my javascript codes after a redirect. It also have some incompabilities with the JQuery ready function. As a provisory solution, I disabled it for this especific case, and everithing is working fine now. – rwehresmann Nov 18 '16 at 01:13
  • I am not every comfortable with Rails. May be this link helps you http://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links. Also, if there is some problem with jQuery, then you can directly use javaScript onload() event – Vikas Sachdeva Nov 18 '16 at 01:22

1 Answers1

1

Put the event binding code inside jQuery ready() function. It will ensure that your event will be bound only after loading of jQuery -

    <script text="text/>JavaScript">
         $(document).ready(function() { 
             $(window).on('load', function(){
                $('body').addClass('background-image')
            });
        });
   </script>
Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26
  • Only for future reference: This answer solved my problem. But, underneath everything, this was not a weird problem from javascript, but a particularity of a Rails lib that I were using in my application. More information are in the question comments. – rwehresmann Nov 18 '16 at 01:22