Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [Solved] Running jQuery code in a .load()’ed page Re: [Solved] Running jQuery code in a .load()’ed page

#75512
Chris Coyier
Keymaster

When you load content into a page dynamically, which is what is happening when you load your homepage, go to another page, and come back, the dynamically loaded content doesn’t run any of the JavaScript inside of it automatically. You’ll need to re-bind any events attached to those objects brought in. You can do that by manually rebinding events in the load functions callback function.

Code:
.load(url, function() { //rebind stuff });

OR, you can do all your binding on the homepage using the .live() function to bind events, which should persist even with dynamically loaded content.

OR, you could just shut off the JavaScript loading fancy stuff and just let each page load with a page refresh as normal (which is what was suggested in the original article). Then you don’t have to deal with rebinding events, as the page will load and run JavaScript as regular for each page.