jona
jona

Reputation: 702

Run Jquery again after the page loaded something new

I wanted my code to run again after some new data is being loaded into the page itself. For example:

$(document).ready(function() {
    console.log("page completed"); 
} 

Once the page is loaded completely it will print page completed in the console. Is there any way to re-run the code when new things have been loaded into the page?

Case #1: On the Facebook news feed, when a user scrolls down the page, JavaScript is run to retrieve the new Facebook feeds and updates.

Case #2: Re-run the scripts when there's changes on the URL itself.

Upvotes: 0

Views: 225

Answers (2)

31piy
31piy

Reputation: 23859

In addition to @Bastiaanus' answer of having a common function, and assuming that your new content is loaded via jQuery's AJAX, you can configure a global handler to run some code every time some content is loaded, using .ajaxSuccess().

If you want to handle more such events, refer to this link.

Upvotes: 1

Bastiaanus
Bastiaanus

Reputation: 357

You can replace your anonymous function with a normal function like this

function onDocumentReady() {
    // Your code
}

Then use that in $(document).ready(onDocumentReady) and later call it again where needed.

Upvotes: 2

Related Questions