Yii is not auto including jquery

13,886

Solution 1

It seems like you're not registering jQuery in the correct way. You must add the following line in /protected/views/layout/main.php before </head> tag:

<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>

It will load jQuery automatically. Remember, if the YII_DEBUG flag is on it will load non-minified version, when deactivated (on production) it will load minified version. Hope this will fix your problem.

Solution 2

If all is working on your local machine it might be a permission problems

Have you checked that /assets is writable on your server ?

Yii publishes the assets it needs at runtime in this directory

Share:
13,886
Alexei Tenitski
Author by

Alexei Tenitski

Web Applications Development Specialist Platform / SRE / DevOps Cloud / AWS / Docker / Kubernetes High Availability / Scaling Microservices / Architecture / Routing CI/CD Go / PHP / Python My strength as a Platform Engineer is that prior to specialising in platform I spent over a decade working as Backend Engineer. This experience and skills allows me to build complex solutions and moving parts to convert monolitical apps to microservices or take more traditional apps to the cloud. I have a passion for improving the efficiency/experience of engineers by creating a better tooling and eliminating bottlenecks to let engineers focus on their task and not get distracted by anything else.

Updated on July 29, 2022

Comments

  • Alexei Tenitski
    Alexei Tenitski over 1 year

    I have a strange problem with Yii & Jquery:

    When I open a page which uses ajax/jquery on localhost Yii does automatically add asset include calls to the of the page:

    <link rel="stylesheet" type="text/css" href="/PATH/assets/f72b359d/style.css" />
    <script type="text/javascript" src="/PATH/assets/2e442e1a/jquery.js"></script>
    <script type="text/javascript" src="/PATH/assets/2e442e1a/jquery.cookie.js"></script>
    

    However when I run same code on the server Yii does not do it hense no Jquery available.

    I'd appreciate if someone could direct to a way to solve it.

  • acorncom
    acorncom about 12 years
    Actually the location of that line doesn't matter. You can place it in a controller, in a view, etc. But I don't think that is the problem here
  • Alexei Tenitski
    Alexei Tenitski about 12 years
    That was the first thing I have checked and all directories are writable.
  • Alexei Tenitski
    Alexei Tenitski about 12 years
    That was the solution. I did not explicitly register jQuery as for some reason it was working fine so I didn't even know I should do it. It have just occurred to me that it was working because Yii Debug Toolbar was including jQuery for me and that toolbar was switched off on the server.