php - Is it possible to link JavaScript files using <script src=""> in view files in yii 2

323

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?

590

Answer

Solution:

yii2 provides registerJsFile() function to inlucde js files.

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

And Yes you can include the js files withscript tag.

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

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

830

Answer

Solution:

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.

People are also looking for solutions to the problem: php - Date format coming in weird way in CSV file using codeigniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.