It was hard for me to find a way to load jQuery
or other CORE scripts in Yii 2
.
In Yii 1
it seemed like this:
<?php Yii::app()->clientScript->registerCoreScript("jquery"); ?>
In Yii 2, the $ application is a property of Yii, not a method, so naturally this does not work, but changes it to:
<?php Yii::$app->clientScript->registerCoreScript("jquery"); ?>
causes this error:
Getting unknown property: yii\web\Application::clientScript
I could not find the documentation for Yii 2 on loading basic scripts, so I tried the following:
<?php $this->registerJsFile(Yii::$app->request->baseUrl . '/js/jquery.min.js', array('position' => $this::POS_HEAD), 'jquery'); ?>
While this loads jQuery into the head, the second version of jQuery also loads Yii if necessary and therefore causes conflicts.
Also, I donโt want to use the Yii jQuery implementation, I would prefer to keep my own and, therefore, thatโs why I do it.
How can I load jQuery and other kernel files without Yii, loading duplicates of them when they need them?
source share