S
S
Sergey2015-03-02 21:26:20
Yii
Sergey, 2015-03-02 21:26:20

How to set assets for yii2 only for a specific page?

Here I have a js script that I want to see only on the main page.
How can i do this?

public $js = [
        'js/jsquery-2.1.0.min.js',
        'js/jquery.mobile-1.4.2.min.js',
        'js/script.js',
    ];

connects to all pages.
This, of course, is good and everything will work.
But I want feng shui.
Do not drag onto the page what will be useless on it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Mokeev, 2015-03-03
@butteff

In the init function, check the url, if the main one is to add

public function init()
{
    if (\yii::$app->request->url == \yii::$app->homeUrl) {
        $this->js[] = 'script.js';
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question