Call JQuery function each time the page is shown

rwehresmann

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?

Vikas Sachdeva

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Call a node function on each page

Call 1 time a function each day

How to change page and call function at the same time?

How call variable outside function .each with Jquery?

JQuery: call function after async each

how to iterate one time over a loop each time call a function?

Run Jquery Function for each table on page

Play multiple videos on page with jQuery each() function

How to redirect to a page and then execute a jQuery function call

jquery .load() after page call function

Execute jQuery function each time scroll on div

How to make a network call to an endpoint each time a page loads

Flutter how to call a function when first page of PageView.builder is shown?

Each Recyclerview item is shown in a whole page

Using Jquery .each() function to hide images as another image is shown in the same class

Next js - Call a function on server side each time a route is changed

rand() produces the same result on each function call (with srand(time(0))

Does PHP max_execution time resets on each function call?

How to call function each time model changes in angular 2?

Call an route on each page

Execute jQuery function each time JS function is triggered

Nativescript angular 2 call Function On page load every time

How can json data be shown in jquery $.each

Call function when hidden element is shown

How to call a separate javascript function in jquery $.each(...) instead of inline?

Call jQuery function for each checkbox on Check/Uncheck All

jQuery each in each function

How to execute the same javascript function on each page loaded with jquery mobile

jQuery ".each()" function only applying script to last class on page