Laravel - Javascript can not find images when path changes
As @ITroubs pointed out, if you have the javascript code inside a blade template (<script>
tag) you can and should use asset()
to generate the URLs. But don't forget the extra quotes so javascript interprets it as string.
$.backstretch([ "{{ asset('img/header-tt-big.jpg') }}", "{{ asset('img/header-soccer-big.jpg') }}"]
However if you have the javascript code in separate .js files (which is actually recommended) you can't do that. Instead I suggest you define a assetBaseUrl
in your layout blade template before the javascript code that needs it:
<script> var assetBaseUrl = "{{ asset('') }}";</script>
Usage:
$.backstretch([ assetBaseUrl + "img/header-tt-big.jpg", assetBaseUrl + "img/header-soccer-big.jpg"]
If you use blade then write the backstretch like this:
$.backstretch([ "{{asset("img/header-tt-big.jpg")}}", "{{asset("img/header-soccer-big.jpg")}}"]
Okay I added the script as inline in a script-tag. But i did put it in a seperate file like p1-inline.blade.php. So i can just include that one. Thank you ;)