1

First off, I have to precise that I'm quite new to Laravel and that I have a lot to learn.

In one of my controller I need to call a Javascript function. To do so, I simply

echo "<script>triggerPixel();</script>";

I'm just unsure about where to store my Javascript file containing that function.

From what I got for the moment is that I should use elixir to add a file with something like this in the gulpfile.js :

mix.scripts('myscript.js');

Is this the right way to do it or should I simply add a file in resources/assets/javascripts ? I'm a bit lost and some explanation would be welcome.

Thanks !

MTH
  • 53
  • 2
  • 6
  • if you're new to laravel, I highly suggest all of these videos: https://www.youtube.com/playlist?list=PLwAKR305CRO-Q90J---jXVzbOd4CDRbVx – Philipp Sander Jul 20 '17 at 09:17
  • When you call any javascript function or include js its not work. You should have to include all the js in footer before

    tag end.

    – Anand Pandey Jul 20 '17 at 09:53

1 Answers1

2

Put your actual .js file inside public/js folder

and then, put all your js reference in one file, for example footer.blade.php inside the resources/views folder.

Inside footer.blade.php :

<script src="{{ URL::asset('js/jquery-3.2.1.min.js') }}"></script>
<script type="text/javascript" src="{{ URL::asset('js/custom.js') }}"></script>
<script type="text/javascript" src="{{ URL::asset('js/app.js') }}"></script>

and then just call the footer inside one of your blade view using: @include ('footer')

derrysan7
  • 457
  • 1
  • 5
  • 15
  • Thanks for your anwser. The problem is that I'm not using a view, I'm only using a controller to process something and during that process I need to call my JS file. – MTH Jul 20 '17 at 09:14
  • This is one of the article saying that you should not call a javascript function inside controller: https://stackoverflow.com/questions/37879258/calling-javascript-function-from-laravel-controller-and-return-data – derrysan7 Jul 20 '17 at 09:18