0

So, I am trying to load a js file into a page, the file does not load when I use simple HTML <script> tag, for example

<script src="js/laravel.js"></script>

But it loads when I use blade,

{!! Html::script('js/laravel.js') !!}

But the tricky part is, in some pages the simple HTML <script> loads the js file fine but in some it does not work at all.

3
  • 1
    Are you sure it's in the public folder? Commented May 16, 2017 at 9:49
  • All depends on current url and directory where js file is placed. In simple HTML script tag are using relative url to js file. So it always is linking to current folder/file. If you are on link domain.com it is looking for laravel.js in "webroot/js" folder. If you have url domain.com/subfolder/, it is looking for js file in folder "webroot/subfolder/js". Commented May 16, 2017 at 9:51
  • yes, it's in the public folder. and about the different folders, every page i create uses the same layout (where the js file is loaded), but why does some pages load the js file but some does not, this is bugging me Commented May 16, 2017 at 10:17

1 Answer 1

3

To access your asset path, you can use asset function helper:

<script src="{{ asset('js/laravel.js') }}"></script>
Sign up to request clarification or add additional context in comments.

3 Comments

yes, I knew that, thanks for answering, but I'm confused that without using the asset function, why is the js file not available for all the pages but some even though all the pages uses the same layout.
It's because the you want to access the asset using relative path. Let's say you're in path /user/1, then the browser will try to get your asset => /user/1/js/laravel.js. Notice that you didn't prepend / in your asset path. Try prepend / character like: <script src="/js/laravel.js"></script>, I think it would work.
hmmm..yeah, that seems to be the case, does work with a / . Thank you for the info, that was really helpful.

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.