How to call a function JavaScript from a specific js file| yii2

Hi!. i have a js file in a draft (wamp/www/test/frontend/web/js/prueba/prueba.js) successfully registered.

Now i want to call a function Rest() (function in the prueba.js file). on a click of a button. or if possible call this function with the page load. example:


       do stuff       

} else {

       call my fuction in the prueba.js file


i am a newbie… and i don’t know how to do. i hope you can help me. Thanks! ?

Register the script. If you don’t register it you will get xxx function is not defined in your console because the view is rendering in the middle of your main layout which should have your main script files. By registering your script in the view , Yii will add your inline script where you have

<?php $this->endBody(); ?>

in your main layout thus, putting your script after your js file and making it valid function call.

in your view you will have to do something like this

<?php $this->registerJs('if($var){

 do stuff 

} else {