Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

external javascript not running in Laravel 5

In Laravel 5, I set up a simple external js file inside public/js/test.js It has one line of code:

alert(1);

I included the file like so in view:

{{Html::script('js/test.js')}}

When the page load, I got

Uncaught SyntaxError: Unexpected token ILLEGAL

error from that javascript file.

I tested running the same test.js file in my MAMP setup, and it run fine. So, the file has no issue. I cannot figure out what in Laravel 5 is causing the issue.

Please help.

like image 587
user32368 Avatar asked Mar 28 '26 12:03

user32368


1 Answers

Put the .js file into the public directory and call it with Laravel's asset() like so:

<script type="text/javascript" src="{{ asset('js/bootstrap/bootstrap.min.js') }}"></script>

If you are using it in view make the file name with .blade.php extension ex: myjsfile.blade.php and call it with an include in any of your view files

@include('myjsfile.blade.php')

Hope that helps

like image 73
max234435 Avatar answered Mar 30 '26 01:03

max234435



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!