0

In my Laravel 9 project that I am working on, I want to put a javascript file containing common functions in app.blade.php. As per How to include External CSS and JS file in Laravel 5.5, I placed the js file in public/js folder and included the following in app.blade.php

 <!-- common js functions added by Ravi  -->
<script src="{{ asset('js/common_js.js') }}" ></script>

However, this file is not getting included on the page, and the js functions included in the file are not available in console.

Could someone advise me how to include a js file so that it is available for all pages?

Thanks.

user761100
  • 2,171
  • 4
  • 23
  • 31
  • please check the following [answer](https://stackoverflow.com/a/44816861/11236563) maybe its the same issue – Omar Tammam Apr 09 '22 at 01:02
  • Omar, I looked at the answer you referred to. That is an old question, perhaps applicable to old versions of Laravel. One more observation: When I update my app.blade.php, it does not refresh, in spite of using "npm run dev" as well as clearing browser cache and running php artisan cache:clear, php artisan config:cache; etc – user761100 Apr 09 '22 at 14:37

3 Answers3

1

if you are using laravel vue do this

place th common_js.js file in resources folder than change this

//in webpack.mix.js
mix.js(["resources/js/app.js","resources/js/common_js.js"], "public/js")

and in app.blade.php use this

<script src="{{ mix('js/app.js') }}" defer></script>

than

npm run dev
Gev99
  • 173
  • 5
  • @user761100 than do this – Gev99 Apr 09 '22 at 15:01
  • I updated webpack.mix.js to include my javascript file. After this, I used npm run dev, and I noticed that javascript functions from my file are included in public/js/app.js. In the second part of your instructions, I believe that you meant to include the script in app.blade.php (and not app.blade.js). This file already includes therefore do I need to . However, I am still not able to use the functions defined in a Vue component. Any suggestion? – user761100 Apr 09 '22 at 19:20
  • I have noticed that the javascript functions I included in are available. Not sure why they were not available earlier. May be the cache issue? – user761100 Apr 23 '22 at 19:23
0

run command

php artisan view:clear
Gev99
  • 173
  • 5
0

you need to check the path rendered by asset() if it's not the correct path you can change the asset path from the .env file ASSET_URL

Ahmed Hassan
  • 579
  • 2
  • 6
  • I don't think I should be changing the ASSET_URL. Rather, I would rather like to put the file in required (correct) folder. On my local system, when I use asset('js/common_ravi.js'), it displays "http://localhost/js/common_ravi.js". Not sure if this is correct. – user761100 Apr 09 '22 at 14:27
  • when you open http://localhost/js/common_ravi.js did you get the file content? if not please check if the files exist in `public/js` – Ahmed Hassan Apr 09 '22 at 14:33
  • Well not with localhost, but with http://127.0.0.1:8000/js/common_ravi.js, I do see the file contents in browser. The path for my javascript file is /Users/Home/laravel_apps/cota/public/js/common_ravi.js (My application's root folder is /Users/Home/laravel_apps/cota. So I believe that everything is at correct location. – user761100 Apr 09 '22 at 14:45
  • try to correct your app URL in the `.env` file change `APP_URL=http://localhost` to `APP_URL=http://127.0.0.1:8000` – Ahmed Hassan Apr 09 '22 at 14:48
  • Ahmed, I made the change you suggested. Now asset('js/common_ravi.js') returns "http://127.0.0.1:8000/js/common_ravi.js" However, I still don't see the file in "view-source". I have noticed that page source is not reflecting changes to app.blade.php in spite of using npm run dev and clearing browser cache as well as php artisan cache:clear. Perhaps that is the issue...But why the changes are not getting reflected. – user761100 Apr 09 '22 at 15:08
  • do you have some kind of adBlocker in your browser? xD if the file path is correct and the URL is correct then check the adblocker xD – Ahmed Hassan Apr 09 '22 at 15:12
  • I don't think so. This is a new mac that I got two weeks ago. I have not installed anything related to ad blocking. – user761100 Apr 09 '22 at 15:49