0

If I have a function, that call another function from index.js file. Why show this error?

<!-- page js -->
<script type="text/javascript" src="{{ asset('assets/js/index.js')}}" ></script> 
<script>
  function onedevice(imei) {
    // mostramos las graficas, se desean datos
    $('#charts_device1').show();
    $('#charts_device2').show();

    // rellenamos los datos
    // var imei = $("#select_imeis").val();
    if (imei !== 0) {
      load_chart_device(imei); // < this line
    }
  }
</script>

The error is:

Uncaught ReferenceError: load_chart_device is not defined
at onedevice (report_devices:302)
at HTMLDocument. (report_devices:234)
at mightThrow (jquery-3.2.1.js:3583)
at process (jquery-3.2.1.js:3651)

I change my code in index.js. But still not working.

function load_chart_device(imei){

console.log(imei);

//CODE....
 });




 function onedevice(imei){
 // mostramos las graficas, se desean datos

 $('#charts_device1').show();
 $('#charts_device2').show();
 // rellenamos los datos
 // var imei = $("#select_imeis").val();
 if(imei!==0){
    load_chart_device(imei);
  }

}

Miguel Herreros Cejas
  • 664
  • 1
  • 12
  • 28

1 Answers1

1

This is happening because function onedevice() is executing first. Write importing of index.js inside html head tag

Write your index.html like this

<!DOCTYPE html>
<html>

<head>
    <title></title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script src="index.js"></script> 

</head>

<body>
    <script type="text/javascript">
        var imei = 10;

        function onedevice(imei) {
            // code ...
            if (imei !== 0) {
                load_chart_device(imei);
            }
        }

        // call onedevice
        onedevice(imei);
    </script>
</body>

</html>

and write your index.js like this

function load_chart_device(imei) {
    console.log("load_chart_device is working fine");
    alert(imei);
}

Make sure you have imported index.js inside <head></head> tag.

To know further about how it works please read Load and execution sequence of web

spyshiv
  • 178
  • 1
  • 8