How to load JavaScript (jQuery) on pjax:ready and document.ready

1.4k views Asked by At

I am using pjax for most of my pages. For some of them I need to include jQuery scripts.

When I load a page with pjax all scripts in $(document).on('ready pjax:success', function() { ... } are working fine, although if the page is loaded directly scripts in above function will not work, and other way round - scripts inside $(document).ready(function() {...} wont work if page is loaded with pjax.

Is there a way to detect if the page was loaded with pjax and use document.ready if it wasn't?

Thanks for all help in advance.

1

There are 1 answers

0
brad On

See https://api.jquery.com/ready/

There is also $(document).on( "ready", handler ), deprecated as of jQuery 1.8 and removed in jQuery 3.0.

My quick fix was this:

function myFunction() { ... }
$(myFunction)
$(document).on('pjax:end', myFunction)