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.

1
  • unfortunately, i didn't use homestead. Do you know what I might be missing? Commented Mar 24, 2016 at 3:53

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

8 Comments

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.
It will work for sure, just inside the myjsfile.blade.php start the file with <script type="text/javascript"> and end the file with </script> and laravel will take that code and include it in the view that you want.
It will still give me an inline script, whereas I want to call an external script.
if you want an external script, you have to put your js file inside the public folder and then call it with this <script type="text/javascript" src="{{ asset('js/bootstrap/bootstrap.min.js') }}"></script> that will get you an external script. Let me know if thats what youre looking for
I still get "Uncaught SyntaxError: Unexpected token ILLEGAL" error. that did not solve it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.