All javascript won't work after Pjax reloads in Yii2
Home » Blog » Yii2.0 framework » All javascript won’t work after Pjax reloads in Yii2

All javascript won’t work after Pjax reloads in Yii2

Updated:   Yii2.0 framework 1 min read

Your support helps keep this blog running! Secure payments via Paypal and Stripe.


Yii version 2.0.13.1

When you work with Pjax either Pjax jquery or Pjax module, you may face the same issue as me. I am working on the Pjax widget in Yii2.0. The widget uses Pjax jquery.

Problem
Every time the Pjax reloads, all js that attached to DOM in Pjax container won’t work.

Cause
After Pjax finishes reloading, the js that set to DOM in Pjax container will not pick up on these changes.

Solution
You must reinitialize the js to DOM after Pjax reloads.

Example

jQuery(document).ready(function ($) {
    function init(){
        initialize plugin1; 
        initialize plugin2;
    }
    init();
    
    $(document).on('ready pjax:end', function (event) {
        init();
    });
    
});

More detail
The instruction can be found in Pjax jquery link.


Your support helps keep this blog running! Secure payments via Paypal and Stripe.


Senior WordPress Developer (Freelancer)

Senior WordPress Developer (Freelancer)

I’m a professional WordPress and WooCommerce developer based in Chiang Mai, Thailand, with over a decade of experience creating fast, secure, and scalable websites. From custom themes and plugins to full WooCommerce stores, I help businesses build a strong and reliable online presence. Need a freelance WordPress developer you can count on? View my portfolio or get in touch to discuss your project.