3

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.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
user32368
  • 33
  • 1
  • 4

1 Answers1

2

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

max234435
  • 587
  • 5
  • 18
  • I've tried that. the js file is getting called. I think referencing to the file is not a problem. but i will try your suggestion with @include. – user32368 Mar 24 '16 at 03:56
  • It will work for sure, just inside the `myjsfile.blade.php` start the file with `` and laravel will take that code and include it in the view that you want. – max234435 Mar 24 '16 at 12:43
  • It will still give me an inline script, whereas I want to call an external script. – user32368 Mar 24 '16 at 15:00
  • if you want an external script, you have to put your js file inside the public folder and then call it with this `` that will get you an external script. Let me know if thats what youre looking for – max234435 Mar 25 '16 at 08:47
  • I still get "Uncaught SyntaxError: Unexpected token ILLEGAL" error. that did not solve it. – user32368 Mar 27 '16 at 02:16
  • I thin you have issues in the actual javarscript file. Have a look at this:http://stackoverflow.com/questions/12719859/no-visible-cause-for-unexpected-token-illegal – max234435 Mar 27 '16 at 23:35
  • found a solution to my problem. Instead of running server with "php artisan serve", I now use MAMP server and run the project in the htdocs folder. I would have to move each file to this new project which is troublesome, but now I know I can call external js files. Thanks for your help all along. – user32368 Mar 29 '16 at 03:11
  • Glad to hear that you found a solution! I use a WAMP server and never had any issues calling in outside files. – max234435 Mar 29 '16 at 04:36