L
L
limpompo2019-05-15 12:55:29
Yii
limpompo, 2019-05-15 12:55:29

Js not working on Yii2 site how to fix it?

At the moment, all scripts work through the layouts/view/index.php file

<script>
    	$( ".catalog" ).dcAccordion();
  </script>

<script>
    function showCart(cart){
  $('#cart .modal-body').html(cart);
  $('#cart').modal();
}
</script>

<script>
function clearCart() {
   $.ajax({
       url: '/cart/clear'
       type: 'GET',
       success: function (res) {
       	if(!res) alert ('Ошибка');
       	showCart(res);
           },
       error: function () {
         alert('Error!');
           }
           });
       }
</script>


  <script> 
$('.add-to-cart').on('click', function (e) {
     e.preventDefault();
     	   var id = $(this).data('id');
     
     $.ajax({
       url: '/cart/add',
       data: {id: id},
       type: 'GET',
       success: function (res) {
       	if(!res) alert ('Ошибка');
       	showCart(res);
       	 //  console.log(res);
           },
       error: function () {
         alert('Error!');
           }
     });
   });
  </script>

If you insert all scripts under one script tag, then they do not work, if you throw scripts in main.js, they do not work either, here is the file in which js is connected
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\assets;

use yii\web\AssetBundle;

/**
 * Main application asset bundle.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
       // 'css/bootstrap.min.css',
        'css/font-awesome.min.css', 
        'css/prettyPhoto.css',
        'css/price-range.css', 
        'css/animate.css', 
        'css/main.css',
        'css/responsive.css', 
    ];
    public $js = [
   //     'js/jquery.js',
     //   'js/bootstrap.min.js',
        'js/jquery.scrollUp.min.js',
        'js/price-range.js',
        'js/jquery.prettyPhoto.js',
        'js/main.js',
        'js/jquery.cookie.js',
        'js/jquery.accordion.js',
    ];

    public $jsOptions = [
        'position' => \yii\web\View::POS_HEAD,
    ];

    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapPluginAsset',
    ];
}

Am I not connecting correctly? There are no repetitions anywhere when connecting
, here is the page code
<html lang="ru-RU">
<head>
<meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>E_Shopper</title>
    <meta name="keywords" content="">
<meta name="description" content="">
<meta name="csrf-param" content="_csrf">
    <meta name="csrf-token" content="MeLmDHeyzMTjETaxVJj-xuySCaLZ1gmjGqsLaS3PaDx1gJR8RPijidRyU8UE863ygKQ9y4iVUcItxkomV5kDCA==">

<link href="/assets/e1b9628f/css/bootstrap.css" rel="stylesheet">
<link href="/css/font-awesome.min.css" rel="stylesheet">
<link href="/css/prettyPhoto.css" rel="stylesheet">
<link href="/css/price-range.css" rel="stylesheet">
<link href="/css/animate.css" rel="stylesheet">
<link href="/css/main.css" rel="stylesheet">
<link href="/css/responsive.css" rel="stylesheet">
<!--[if lte IE9]>
<script src="js/html5shiv.js"></script>
<![endif]-->
<!--[if lte IE9]>
<script src="js/respond.min.js"></script>
<![endif]-->
<script src="/assets/fec24831/jquery.js"></script>
<script src="/assets/3dda746e/yii.js"></script>
<script src="/assets/e1b9628f/js/bootstrap.js"></script>
<script src="/js/jquery.scrollUp.min.js"></script>
<script src="/js/price-range.js"></script>
<script src="/js/jquery.prettyPhoto.js"></script>
<script src="/js/main.js"></script>
<script src="/js/jquery.cookie.js"></script>
<script src="/js/jquery.accordion.js"></script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Mokeev, 2019-05-16
@zetamen

public $js = [
   //     'js/jquery.js',
     //   'js/bootstrap.min.js',
        'js/jquery.scrollUp.min.js',
        'js/price-range.js',
        'js/jquery.prettyPhoto.js',
        'js/main.js',
        'js/jquery.cookie.js',
        'js/jquery.accordion.js',
    ];

main.js should be at the end of the array since the order of the scripts is important
Scripts are included in the head, so everything in main.js needs to be wrapped in document.ready

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question