2

Firstly, I developed yii 1 applcation. In the application, I linked js files like this:

<script src="<?php echo Yii::app()->theme->baseUrl;?>/Index/script_nav.js"></script>

Is it possible to link JavaScript files using in yii 2 or should I link js files using appasset?

3
  • there must be a framework way to embed js & css Commented Sep 16, 2016 at 11:05
  • yes you can add Commented Sep 16, 2016 at 11:07
  • see this yiiframework.com/doc-2.0/… Commented Sep 16, 2016 at 11:16

2 Answers 2

1

yii2 provides registerJsFile() function to inlucde js files.

<?php $this->registerJsFile('js/company-index.js'); ?>

And Yes you can include the js files with script tag.

<script src="<?php echo Yii::$app->request->baseUrl;?>/js/youFile.js"></script>

but you should use registerJsFile, See this answer : https://stackoverflow.com/a/28142370/3143384

Sign up to request clarification or add additional context in comments.

Comments

0

You can use that in layout file

Path

views->layout->main.php

Like simple as

 <script src="myscripts.js"></script> 

For checking its linked or not use inspect element -> network (open this before landing page)

Here you can see your files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.