Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put the page-initialize javascript when using pjax?

Most of the times, I put some javascript code in $(document).ready to do some initialization stuffs on the page, like event binding, etc.

But now I would like to use pjax https://github.com/defunkt/jquery-pjax for some of my pages.

With pjax, because there's only part of the page gets refreshed, $(document).ready will not get called again.

I could manually trigger the initializing script on event pjax:end, but I also want to know if there's a better solution for that.

Thanks.

like image 906
larryzhao Avatar asked Mar 14 '12 04:03

larryzhao


1 Answers

You can easily link all your existing code to both the document.ready and pjax:success events, like so:

Before:

$(document).ready(function() {
    // page load stuff
});

After:

$(document).on('ready pjax:success', function() {
    // will fire on initial page load, and subsequent PJAX page loads
});
like image 109
Adrian Macneil Avatar answered Oct 11 '22 18:10

Adrian Macneil